参考原文: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
- }
参考上面的使用进行如下运用举例:
void PageListResponse()
{
cJSON * pJsonRoot = NULL;
cJSON * nameList = cJSON_CreateArray();
int i=;
cJSON *pJsonSub=NULL; pJsonRoot = cJSON_CreateObject(); if(NULL == pJsonRoot)
{
//error happend here
return ;
} if(NULL == nameList)
{
// create object faild, exit
printf("create json array faild\n");
return ;
} cJSON_AddStringToObject(pJsonRoot, "cmd", "PageListRes");
cJSON_AddItemToObject(pJsonRoot, "params", nameList); if(gPlayInfo.listinfo.audioInputSource==WIFI)
{
for(i=;i<LISTNAMENUMMAX;i++)
{ cJSON_AddItemToArray(nameList,pJsonSub= cJSON_CreateObject());
//cJSON_AddStringToObject(pJsonSub, "name",(gPlayInfo.listbuf+i)->name); //实验这种方式也OK,相对而言更简便!
//cJSON_AddStringToObject(pJsonSub, "singer",(gPlayInfo.listbuf+i)->singer);
cJSON_AddItemToObject(pJsonSub, "name", cJSON_CreateString((gPlayInfo.listbuf+i)->name));
cJSON_AddItemToObject(pJsonSub, "singer", cJSON_CreateString((gPlayInfo.listbuf+i)->singer));
}
}
else if(gPlayInfo.listinfo.audioInputSource==USB || gPlayInfo.listinfo.audioInputSource==SD)
{
for(i=;i<LISTNAMENUMMAX;i++)
{
//pJsonSub = cJSON_CreateObject();
cJSON_AddItemToArray(nameList,pJsonSub = cJSON_CreateObject());
//cJSON_AddStringToObject(pJsonSub, "name", paginglistname[i]); //这种方式也相当下面的实现,也OK
//cJSON_AddStringToObject(pJsonSub, "singer",“NULL”); //注意为"NULL" cJSON_AddItemToObject(pJsonSub, "name", cJSON_CreateString(paginglistname[i]));
cJSON_AddItemToObject(pJsonSub, "singer", cJSON_CreateString("NULL")); //注意空字串为“NULL”,而不是NULL,开始调试的时候,写入NULL,则出现EPC。 }
} char * p = cJSON_Print(pJsonRoot); // 将Json数组转成字符串指针,会产生内存,注意用free释放内存; if(NULL == p)
{
cJSON_Delete(pJsonRoot);
return;
} mosquitto_pub_start(p); cJSON_Delete(pJsonRoot); //不管创建了多少层,不需要一个一个进行删除,用cJSON_Delete删除根节点就ok
free(p); return ;
}
输出结果如下格式:
{
"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); }