C语言创建及解析Json的使用法则
参考原文:http://blog.csdn.net/xukai871105/article/details/33013455
JSON(JavaScriptObject Notation)是一种轻量级的数据交换格式。它基于JavaScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
cJSON结构体:
typedefstruct cJSON {
structcJSON *next,*prev;
struct cJSON *child;
int type;
char * valuestring;
int valueint;
double valuedouble;
char *string;
}cJSON;
几点说明
- void create_single_number(void) {
- cJSON *root;
- char *out;
- int value = 24;
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "value", cJSON_CreateNumber(value));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "value": 24
- }
- #endif
- }
- void create_single_string(void) {
- cJSON *root;
- char *out;
- char *name = "xukai871105";
- root = cJSON_CreateObject(); // 创建根
- // 方法 使用cJSON_AddItemToObject,推荐使用
- cJSON_AddItemToObject(root, "name", cJSON_CreateString(name));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "name": "xukai871105"
- }
- #endif
- }
- void create_bool(void) {
- cJSON *root;
- char *out;
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "success", cJSON_CreateFalse());
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "success": false
- }
- #endif
- }
- void create_simple(void) {
- cJSON *root;
- char *out;
- int list[4] = {5,6,7,8};
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddItemToObject(root, "lists", cJSON_CreateIntArray(list, 4));
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "lists": [5, 6, 7, 8]
- }
- #endif
- }
- void create_complex(void) {
- cJSON *root, *rows, *row;
- char *out;
- int i = 0;
- char *title[3] = { "树莓派学习笔记——索引博文",
- "树莓派学习笔记——GPIO功能学习",
- "物联网学习笔记——索引博文"};
- char *url[3] = { "http://blog.csdn.net/xukai871105/article/details/23115627",
- "http://blog.csdn.net/xukai871105/article/details/12684617",
- "http://blog.csdn.net/xukai871105/article/details/23366187"};
- root = cJSON_CreateObject(); // 创建根
- cJSON_AddNumberToObject(root, "total", 3);
- // 在object中加入array
- cJSON_AddItemToObject(root, "rows", rows = cJSON_CreateArray());
- for(i = 0; i < 3; i++) {
- // 在array中加入object
- cJSON_AddItemToArray(rows, row = cJSON_CreateObject());
- cJSON_AddItemToObject(row, "title", cJSON_CreateString(title[i]));
- cJSON_AddItemToObject(row, "url", cJSON_CreateString(url[i]));
- }
- // 打印并释放
- out = cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out);
- // 控制台输出
- #if 0
- {
- "total": 3,
- "rows": [{
- "title": "树莓派学习笔记——索引博文",
- "url": "http://blog.csdn.net/xukai871105/article/details/23115627"
- }, {
- "title": "树莓派学习笔记——GPIO功能学习",
- "url": "http://blog.csdn.net/xukai871105/article/details/12684617"
- }, {
- "title": "物联网学习笔记——索引博文",
- "url": "http://blog.csdn.net/xukai871105/article/details/23366187"
- }]
- }
- #endif
- }
参考上面的使用进行如下运用举例:
1 void PageListResponse() 2 { 3 cJSON * pJsonRoot = NULL; 4 cJSON * nameList = cJSON_CreateArray(); 5 int i=0; 6 cJSON *pJsonSub=NULL; 7 8 pJsonRoot = cJSON_CreateObject(); 9 10 if(NULL == pJsonRoot) 11 { 12 //error happend here 13 return ; 14 } 15 16 if(NULL == nameList) 17 { 18 // create object faild, exit 19 printf("create json array faild\n"); 20 return ; 21 } 22 23 cJSON_AddStringToObject(pJsonRoot, "cmd", "PageListRes"); 24 cJSON_AddItemToObject(pJsonRoot, "params", nameList); 25 26 if(gPlayInfo.listinfo.audioInputSource==WIFI) 27 { 28 for(i=0;i<LISTNAMENUMMAX;i++) 29 { 30 31 cJSON_AddItemToArray(nameList,pJsonSub= cJSON_CreateObject()); 32 //cJSON_AddStringToObject(pJsonSub, "name",(gPlayInfo.listbuf+i)->name); //实验这种方式也OK,相对而言更简便! 33 //cJSON_AddStringToObject(pJsonSub, "singer",(gPlayInfo.listbuf+i)->singer); 34 cJSON_AddItemToObject(pJsonSub, "name", cJSON_CreateString((gPlayInfo.listbuf+i)->name)); 35 cJSON_AddItemToObject(pJsonSub, "singer", cJSON_CreateString((gPlayInfo.listbuf+i)->singer)); 36 } 37 } 38 else if(gPlayInfo.listinfo.audioInputSource==USB || gPlayInfo.listinfo.audioInputSource==SD) 39 { 40 for(i=0;i<LISTNAMENUMMAX;i++) 41 { 42 //pJsonSub = cJSON_CreateObject(); 43 cJSON_AddItemToArray(nameList,pJsonSub = cJSON_CreateObject()); 44 //cJSON_AddStringToObject(pJsonSub, "name", paginglistname[i]); //这种方式也相当下面的实现,也OK 45 //cJSON_AddStringToObject(pJsonSub, "singer",“NULL”); //注意为"NULL" 46 47 cJSON_AddItemToObject(pJsonSub, "name", cJSON_CreateString(paginglistname[i])); 48 cJSON_AddItemToObject(pJsonSub, "singer", cJSON_CreateString("NULL")); //注意空字串为“NULL”,而不是NULL,开始调试的时候,写入NULL,则出现EPC。 49 50 } 51 } 52 53 54 char * p = cJSON_Print(pJsonRoot); // 将Json数组转成字符串指针,会产生内存,注意用free释放内存; 55 56 if(NULL == p) 57 { 58 cJSON_Delete(pJsonRoot); 59 return; 60 } 61 62 mosquitto_pub_start(p); 63 64 cJSON_Delete(pJsonRoot); //不管创建了多少层,不需要一个一个进行删除,用cJSON_Delete删除根节点就ok 65 free(p); 66 67 return ; 68 } 69 70
输出结果如下格式:
{
"cmd":"PageListRes",
"params":
[
{"name":"",”singer”:””},
{"name":"",”singer”:””},
{"name":"",”singer”:””},
......
{"name":"",”singer”:””},
]
}
========================================================================================
解析Json:
如下例子:
{
"cmd": "play",
"params": {
"listID": "abcdefg", //32位md5值
"playIndex":0 //播放索引
"songList":
[{
"id": "111",
"name": "what name",
"uri": "http://www.xxx.com",
"img": "http://www.xxx.com/yyy.jpg",
"singer": "singerName",
"album": "title11"
},
{
"id": "222",
"name": "what name",
"uri": "http://www.xxx.com",
"img": "http://www.xxx.com/yyy.jpg",
"singer": "singerName",
"album": "title22"
}]
},
}
该json数据指针通过message->payload传递,
cJSON* cmd; char* cmd_out; cJSON* id; char* id_out; cJSON* name; char* name_out; cJSON* singer; char* singer_out; cJSON* param; cJSON* songList; int songNum; cJSON* pArray_root = cJSON_Parse((char*)message->payload);//解析根节点 cmd = cJSON_GetObjectItem(pArray_root,"cmd"); param = cJSON_GetObjectItem(pArray_root,"params"); playIndex = cJSON_GetObjectItem(param,"playIndex")->valueint; songList = cJSON_GetObjectItem(param,"songList"); songNum=cJSON_GetArraySize(songList); int j=0; for(j=0;j<songNum;j++) { pArray = cJSON_GetArrayItem(songList, j); name = cJSON_GetObjectItem(pArray,"name"); uri = cJSON_GetObjectItem(pArray,"uri"); id = cJSON_GetObjectItem(pArray,"id"); singer= cJSON_GetObjectItem(pArray,"singer"); name_out = cJSON_Print(name); uri_out = cJSON_Print(uri); id_out = cJSON_Print(id); singer_out = cJSON_Print(singer); ....... free(name_out); free(uri_out); free(id_out); free(singer_out); }