赞
踩
extern cJSON *cJSON_Parse(const char *value);//从给定的json字符串中得到cjson对象 (PS:把一个字符串数组转换成 JSON数据对象, 每次解析数据之前,必须要看当前的数据是否能转换成JSON数据格式。) 参数一:需要转换的字符串 返回值:转换成功:返回JSON对象 转换失败:返回NULL /*通过KEY去获取对应的value*/ extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//根据键获取对应的值(cjson对象) 参数一:需要获取的对象 (转换后的cJSON对象) 参数二:对应的key值 返回值:成功:返回key对应的value 失败:返回NULL //把value转换成字符串输出 extern char *cJSON_Print(cJSON *item);//从cjson对象中获取有格式的json对象 //获取cjson对象数组成员的个数 extern int cJSON_GetArraySize(cJSON *array); //参数一:需要获取的JSON数组对象 返回值:该对象成员的个数 //根据下标获取cjosn对象数组中的对象 cJSON *cJSON_GetArrayItem(cJSON *array,int item); //参数一:JSON数组对象 //参数二:数组下标号 (从0开始的) 返回值:成功:JSON对象 失败:返回NULL //从cjson对象中获取有格式的json对象 extern char *cJSON_Print(cJSON *item); //从cjson对象中获取无格式的json对象 extern char *cJSON_PrintUnformatted(cJSON *item); //删除cjson对象,释放链表占用的内存空间 extern void cJSON_Delete(cJSON *c); //获取cjson对象数组成员的个数 extern int cJSON_GetArraySize(cJSON *array); //根据下标获取cjosn对象数组中的对象 extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); //根据键获取对应的值(cjson对象) extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); //获取错误字符串 extern const char *cJSON_GetErrorPtr(void);
//应用实例:
{
"display": {
"deviceList": [{
"hostID": 10000,
"name": "a"
}, {
"hostID": 10001,
"name": "b"
}]
},
"version": "1.0"
}
//合成以上数据 #include <stdio.h> #include "cJSON.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { cJSON *hostID_item = NULL; cJSON *nameItem = NULL; cJSON *deviceListArray = cJSON_CreateArray(); cJSON_AddItemToArray(deviceListArray,hostID_item=cJSON_CreateObject()); cJSON_AddItemToObject(hostID_item, "hostID", cJSON_CreateNumber(10000)); cJSON_AddItemToArray(deviceListArray,hostID_item=cJSON_CreateObject()); cJSON_AddItemToObject(hostID_item, "name", cJSON_CreateString("a")); cJSON_AddItemToArray(deviceListArray,hostID_item=cJSON_CreateObject()); cJSON_AddItemToObject(hostID_item, "hostID", cJSON_CreateNumber(10001)); cJSON_AddItemToArray(deviceListArray,hostID_item=cJSON_CreateObject()); cJSON_AddItemToObject(hostID_item, "name", cJSON_CreateString("b")); cJSON *deviceList = cJSON_CreateObject(); cJSON_AddItemToObject(deviceList, "deviceList", deviceListArray); cJSON *root = cJSON_CreateObject(); cJSON_AddItemToObject(root, "display", deviceList); cJSON_AddItemToObject(root,"version",cJSON_CreateString("1.0")); printf("%s\n", cJSON_Print(root)); return 0; }
//解析以上json数据: #include <stdio.h> #include "cJSON.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> char *data = "{\"display\":{\"deviceList\":[{\"hostID\":10000,\"name\":\"a\"},{\"hostID\":10001,\"name\":\"b\"}]},\"version\":\"1.0\"}"; int main() { cJSON *root = cJSON_Parse(data); if(root == NULL) { printf("get root fail!!!\n"); return -1; } else { cJSON *value = cJSON_GetObjectItem(root,"version");//获取键"version"对应的JSON值 if(value == NULL) { printf("get value fail!!!\n"); return -1; } printf("version = %s\n", value->valuestring); value = cJSON_GetObjectItem(root,"display"); if(value != NULL) { value = cJSON_GetObjectItem(value,"deviceList"); if(value != NULL) { int size = cJSON_GetArraySize(value); for(int i=0; i<size; i++) { cJSON *array = cJSON_GetArrayItem(value, i); if(array != NULL) { cJSON *item = cJSON_GetObjectItem(array,"name"); if(item != NULL) { printf("name = %s\n", item->valuestring); } item = cJSON_GetObjectItem(array,"hostID"); if(item != NULL) { printf("hostID = %d\n", item->valueint); } } } } } } return 0; }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。