您的位置:首页 > Web前端 > JavaScript

CJSON数据的解析和合成示例

2017-03-03 11:33 183 查看
解析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语言又捡了回来,希望以后能有更多的东西与大家分享。如有错误之处,欢迎指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息