1、先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h。
copy
<spanstyle="font-family:CourierNew;font-size:12px;">char*json="{"json":{"id":1,"nodeId":11,"deviceId":111,"deviceName":"aaa","ieee":"01212","ep":"1111","type":"bbb"}}";
- char*json1="{"id":1,"deviceName":"aaa"}";
- cJSON*root;
- cJSON*format;
- intvalue_int;
- char*value_string;
-
- root=cJSON_Parse(json);
- format=cJSON_GetObjectItem(root,"json");
- value_int=cJSON_GetObjectItem(format,"nodeId")->valueint;
- value_string=cJSON_GetObjectItem(format,"ieee")->valuestring;
- printf("%dn",value_int);
- printf("%sn",value_string);
- cJSON_Delete(root);
- root=cJSON_Parse(json1);
- value_int=cJSON_GetObjectItem(root,"id")->valueint;
- value_string=cJSON_GetObjectItem(root,"deviceName")->valuestring;
- printf("%dn",value_int);
- printf("%sn",value_string);
- cJSON_Delete(root);</span>
copy
copy
<spanstyle="font-family:CourierNew;font-size:12px;">11
- 01212
- 1
- aaa
- </span>
struct cJSON *next,*prev;// 数组 对象数据中用到
struct cJSON *child;// 数组 和对象中指向子数组对象或值
int type;// 元素的类型,如是对象还是数组
char *valuestring;// 如果是字符串
double valuedouble;// 如果类型是cJSON_Number
char *string;// The item's name string,if this item is the child of,or is in the list of subitems of an object.
"name": "Jack ("Bee") Nimble",
"name": "Jack ("Bee") Nimble",
cJSON *root = cJSON_Parse(my_json_string);
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
char *rendered=cJSON_Print(root);
root=cJSON_CreateObject();
cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack ("Bee") Nimble"));
cJSON_AddItemToObject(root,"format",fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type","rect");
cJSON_AddNumberToObject(fmt,"width",1920);
cJSON_AddNumberToObject(fmt,"height",1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate",24)
原文地址:http://www.52php.cn/article/p-zzasyftz-eh.html (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|