CJSON数据的解析和合成示例

  1. 解析json数据

这里直接引用实际项目中用的json数据示例,一个附带数组的解析。下面这串数据看起来很长,在解析的时候可以放到json在线解析的网页上——json在线解析,可以清楚地看到数据结构,方便我们提取所需要的数据。

{
    "asr_recongize":"周杰伦青花瓷",
    "nluProcessTime":"51",
    "rc":0,
    "text":"周杰伦青花瓷",
    "service":"cn.xxx.music",
    "code":"SEARCH_SONG",
    "semantic":{
        "intent":{
            "artist":"周杰伦",
            "song":"青花瓷",
            "keyword":"周杰伦 青花瓷"
        }
    },
    "data":{
        "result":{
            "count":2,
            "musicinfo":[
                {
                    "id":"1776214565",
                    "title":"青花瓷 (Live)",
                    "artist":"周杰伦",
                    "album":"周杰伦现场合",
                    "duration":234,
                    "url":"http: //om5.alicdn.com/891/1178104891/2100348474/1776214565_60402499_l.mp3?auth_key=9b8445fd617e7d7f438eb71950a96ce2-1482624000-0-null",
                    "imgUrl":"http: //img.xiami.net/images/album/img26/16/58106c29e904a_809326_1477471273_1.jpg",
                    "hdImgUrl":"http: //img.xiami.net/images/album/img26/16/58106c29e904a_809326477471273.jpg",
                    "lyric":"",
                    "errorCode":0
                },
                {
                    "id":"1773859336",
                    "title":"青花瓷/阳光宅男(Live)",
                    "artist":"周杰伦",
                    "album":"2015江苏卫视新年演唱会",
                    "duration":445,
                    "url":"http: //om5.alicdn.com/169/7169/1020552829/1773859336_16147955_l.mp3?auth_key=d8f0ee9e3dd66573d995bad6c9162003-1482624000-0-null",
                    "imgUrl":"http: //img.xiami.net/images/album/img69/7169/10205528291420552829_1.jpg",
                    "hdImgUrl":"http: //img.xiami.net/images/album/img69/7169/10205528291420552829.jpg",
                    "lyric":"",
                    "errorCode":0
                }
            ],
            "updateTime":"2016-12-2412: 16: 38",
            "errorCode":0,
            "dataSourceName":"XIA_MI"
        }
    },
    "general":{
        "type":"T",
        "text":"为您找到如下歌花瓷(Live),青花瓷/阳光宅男(Live)"
    },
    "history":"cn.xxx.music",
    "responseId":"f64e311d8baa42a4a4065afd83114fb7"
}

解析过程:
为方便观察,我略去了很多判断是否为空的代码,这里直接贴出了实际会用到的解析部分。

    root = cJSON_Parse(stringSrc);
    cJSON *general = cJSON_GetObjectItem(root, "general");
    cJSON *general_text = cJSON_GetObjectItem(general, "text");
    cJSON *data = cJSON_GetObjectItem(root, "data");
    cJSON *result = cJSON_GetObjectItem(data, "result");
    cJSON *count = cJSON_GetObjectItem(result, "count");
    cJSON *musicinfo = cJSON_GetObjectItem(result, "musicinfo");
    int i = 0;
    for (i = 0; i < (count->valueint); i++)
        {
                cJSON *musicinfoArray = cJSON_GetArrayItem(musicinfo, i);
                cJSON *album = cJSON_GetObjectItem(musicinfoArray, "album");
                cJSON *artist = cJSON_GetObjectItem(musicinfoArray, "artist");
                cJSON *title = cJSON_GetObjectItem(musicinfoArray, "title");
                cJSON *hdImgUrl = cJSON_GetObjectItem(musicinfoArray, "hdImgUrl");
                cJSON *imgUrl = cJSON_GetObjectItem(musicinfoArray, "imgUrl");
                cJSON *url = cJSON_GetObjectItem(musicinfoArray, "url");
        } 

2.合成json数据

合成代码示例:
这里选择了合成一个铃声列表的json数据,同样附带了数组的生成,以及常用的字符串数据格式。

    cJSON *itemArray;
    cJSON *root = cJSON_CreateObject();
    cJSON *item = cJSON_CreateObject();
    cJSON *alarmInfo = cJSON_CreateArray();
    cJSON_AddItemToObject(root, "alarmRingList", item);
    cJSON_AddItemToObject(item, "alarmInfo", alarmInfo);
    cJSON_AddItemToObject(alarmInfo, "itemArray", itemArray);
    cJSON_AddItemToObject(root,"type",cJSON_CreateString("alarmRingList"));
    cJSON_AddItemToObject(item,"name",cJSON_CreateString("铃声列表"));

    int i=0;
    for(i=0; i<5; i++)
    {
        cJSON_AddItemToArray(alarmInfo,itemArray=cJSON_CreateObject());
        cJSON_AddItemToObject(itemArray, "url", cJSON_CreateString("I am a ringlist example"));
    }
    cJSON_AddItemToObject(item, "count", "5");

合成结果:

{
    "alarmRingList":{
        "alarmInfo":[
            {
                "url":"I am a ringlist example "
            },
            {
                "url":"I am a ringlist example "
            },
            {
                "url":"I am a ringlist example "
            },
            {
                "url":"I am a ringlist example "
            },
            {
                "url":"I am a ringlist example "
            }
        ],
        "name":"铃声列表",
        "count":"10"
    },
    "type":"alarmRingList"
}

例子就列这么多吧,只要尝试着写出了一两个例子,后面的过程都会轻车熟路了。这段时间又在把以前抛弃的c语言又捡了回来,希望以后能有更多的东西与大家分享。如有错误之处,欢迎指正。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 cJSON解析 JSON 字符串并释放内存的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include "cJSON.h" void parseAndFreeJSON(const char* json_str) { // 解析 JSON 字符串 cJSON* root = cJSON_Parse(json_str); if (root == NULL) { printf("Failed to parse JSON: %s\n", cJSON_GetErrorPtr()); return; } // 获取字段的值 cJSON* name = cJSON_GetObjectItem(root, "name"); cJSON* age = cJSON_GetObjectItem(root, "age"); cJSON* city = cJSON_GetObjectItem(root, "city"); if (name == NULL || age == NULL || city == NULL) { printf("Failed to get JSON values\n"); cJSON_Delete(root); // 释放 cJSON 对象 return; } // 打印字段的值 printf("Name: %s\n", name->valuestring); printf("Age: %d\n", age->valueint); printf("City: %s\n", city->valuestring); // 释放 cJSON 对象 cJSON_Delete(root); } int main() { // JSON 字符串 const char* json_str = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"; // 解析并释放 JSON parseAndFreeJSON(json_str); return 0; } ``` 在示例代码中,我们定义了一个函数 `parseAndFreeJSON` 来解析 JSON 字符串并释放相关的内存。首先,使用 `cJSON_Parse` 函数解析 JSON 字符串,得到一个指向根节点的指针。然后,通过 `cJSON_GetObjectItem` 函数获取字段的值。最后,打印字段的值后,我们使用 `cJSON_Delete` 函数释放 cJSON 对象的内存。 这样,您可以在需要解析 JSON 字符串的地方调用 `parseAndFreeJSON` 函数,并在使用完 cJSON 对象后手动释放内存。 希望这个示例对您有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值