cJSON 结构:
typedef struct cJSON {
structcJSON *next,*prev; /* next/prevallow you to walk array/object chains. Alternatively, useGetArraySize/GetArrayItem/GetObjectItem */
structcJSON *child; /* Anarray or object item will have a child pointer pointing to a chain of the itemsin the array/object. */
inttype; /*The type of the item, as above. */
char*valuestring; /* Theitem's string, if type==cJSON_String */
intvalueint; /*The item's number, if type==cJSON_Number */
doublevaluedouble; /*The item's number, if type==cJSON_Number */
char*string; /*The item's name string, if this item is the child of, or is in the list ofsubitems of an object. */
} cJSON;
Object 、item、 array 都是一个cJSON对象
Json读入:
1. 建立json根对象将json字符串解析到跟对象
2. 从跟对象中获取item item可能是 item、object、array
1、 Item: 直接通过item->valueString或item->valueIntitem->valueFloat获得字符串的值、或者整形值浮点型
2、 object:从根对象中获得的item为object ,那么接着从当前object中获得item
3、 array:从跟对象中获得的item为array,那么需要你得到数组长度 然后从数组中获得相应的对象 继续处理
3. 释放json空间
cJSON*root_input=cJSON_Parse(charjson); //将字符串形式的json数据解析给cJSON对象
cJSON*item=cJSON_GetObjectItem(cmdroot_input,"cmd");//向json对象中读取item item可以是一个简单item 也可以是一个json对象 读入json对象或数组用 cJSON*objectson=cmdroot->child 若是json对象就做如上处理
若是数组
intarraylen=cJSON_GetArraySize(objectson);
cJSONtemobj=cJSON_getArrayItem(objectson,i);//i memcpy(cmd_input.cmd,item->valuestring,strlen(item->valuestring)+1); item=cJSON_GetObjectItem(cmdroot_input,"num"); cmd_input.num=item->valueint; cJSON_Delete(cmdroot_input); Json写入: 1. 创建json root对象分配空间 2. 向root对象中添加 item item可以是item 、object 、array 1、item:用cJSON_CreateString() cJSON_CreateNumber()创建的item 2、object:先创建json对象分配空间 可以向当前对象中添加item 3、array:先创建array 分配空间 然后向当前的array添加object 3.生成json root对象 的json字符串 4释放json root空间 Code: int main(int argc,char **argv) { //******write json********************** cJSON*root=cJSON_CreateObject(); cJSON*obj=cJSON_CreateObject(); cJSON*array=cJSON_CreateArray(); inti; //**********************itemteststring:stringtest cJSON_AddItemToObject(root,"teststring",cJSON_CreateString("stringtest")); cJSON_AddItemToObject(root,"testnum",cJSON_CreateNumber(15)); //**********************object cJSON_AddItemToObject(root,"testobject",obj); cJSON_AddItemToObject(obj,"testobjstring",cJSON_CreateString("objstring")); //**********************array cJSON_AddItemToObject(root,"testarray",array); for(i=0;i<5;i++){ cJSON*arrayobj=cJSON_CreateObject(); cJSON_AddItemToArray(array,arrayobj); cJSON_AddItemToObject(arrayobj,"arrayobjnum",cJSON_CreateNumber(i)); } char*temjson=cJSON_Print(root); printf("%s\n",temjson); charjson[255]; strncpy(json,temjson,strlen(temjson)+1); cJSON_Delete(root);//deletethe root //*******readjson******************************** cJSON*rootread=cJSON_Parse(json),*objread,*arrayread; //***************readitem cJSON*itemread=cJSON_GetObjectItem(rootread,"teststring"); printf("teststring:%s\n",itemread->valuestring); itemread=cJSON_GetObjectItem(rootread,"testnum"); printf("testnum:%d\n",itemread->valueint); //***************readobject objread=cJSON_GetObjectItem(rootread,"testobject"); itemread=cJSON_GetObjectItem(objread,"testobjstring"); printf("testobjstring: %s\n",itemread->valuestring); //***************readarray arrayread=cJSON_GetObjectItem(rootread,"testarray"); intarraysize=cJSON_GetArraySize(arrayread); for(i=0;i objread=cJSON_GetArrayItem(arrayread,i); itemread=cJSON_GetObjectItem(objread,"arrayobjnum"); printf("arrayobjnum: %f\n",itemread->valuedouble ); } cJSON_Delete(rootread); return0; }