代码改变世界

CJSON create.c

2018-05-28 23:25  清晨、午后  阅读(312)  评论(0编辑  收藏  举报

 

#include <stdio.h>
#include "cJSON.h"

/*
{
    "semantic": {
        "slots":    {
            "name": "张三"
        }
    },
    "rc":   0,
    "operation":    "CALL",
    "service":  "telephone",
    "text": "打电话给张三"
}

*/
int main()
{
    cJSON * root =  cJSON_CreateObject();
    cJSON * item =  cJSON_CreateObject();
    cJSON * next =  cJSON_CreateObject();

    cJSON_AddItemToObject(root, "rc", cJSON_CreateNumber(0));
    cJSON_AddItemToObject(root, "operation", cJSON_CreateString("CALL"));
    cJSON_AddItemToObject(root, "service", cJSON_CreateString("telephone"));
    cJSON_AddItemToObject(root, "text", cJSON_CreateString("打电话给张三"));
    cJSON_AddItemToObject(root, "semantic", item);
    cJSON_AddItemToObject(item, "slots", next);
    cJSON_AddItemToObject(next, "name", cJSON_CreateString("张三"));

    printf("%s\n", cJSON_Print(root));

    return 0;
}