json文件解析
Posted icefree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json文件解析相关的知识,希望对你有一定的参考价值。
对json文本的解析有两种方法:
第一种:利用 child指针进行访问
- 利用child指针进行访问,需要不断的进行child,以进入深层循环中。
- 访问面文本的slots数组中的成员
-
"semantic": "slots":[ "name":"ZhangSan", "ip":"ZhangSan" , "name":"LiSi", "ip":"ZhangSan" , "name":"WangWu", "ip":"ZhangSan" ] , "rc":0, "operation":"CALL", "service":"telephone", "text":"Call ZhangSan"
解析方法:
-
char * jsonStr = "\"semantic\":\"slots\":[\"name\":\"ZhangSan\",\"ip\":\"ZhangSan\",\"name\":\"LiSi\",\"ip\":\"ZhangSan\",\"name\":\"WangWu\",\"ip\":\"ZhangSan\"], \"rc\":0, \"operation\":\"CALL\", \"service\":\"telephone\", \"text\":\"Call ZhangSan\""; cJSON * root = NULL; cJSON * item = NULL;//cjson对象 root = cJSON_Parse(jsonStr); //解析JSON数据包,并按照cJSON结构体的结构序列化整个数据包。使用该函数会通过malloc()函数在内存中开辟一个空间,使用完成需要手动释放。 if (!root) printf("Error before: [%s]\n",cJSON_GetErrorPtr()); else cJSON *slots_arr = cJSON_GetObjectItem(root->child, "slots"); if( NULL != slots_arr ) cJSON *next ; int i; for(next= slots_arr->child; NULL!=next;)
char * ip = cJSON_GetObjectItem( next , "name")->valuestring ; char * mask = cJSON_GetObjectItem( next , "ip")->valuestring ; printf("ip: %s mask: %s\n",ip,mask); next = next->next ;运行输出:
-
ip: ZhangSan mask: ZhangSan ip: LiSi mask: ZhangSan ip: WangWu mask: ZhangSan
第二种:定义cJSON结构体指针进行访问
- 利用cJSON结构体指针访问,一层一个指针,下一层的cJSON指针等于对上一层的cJSON结构体进行访问,从而获得其成员;
- 对下面的json文件进行访问
-
"port":[ "com3": "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028"] , "com4": "bound": 2400, "device_type": "master", "ammeter_addr": [] , "com5": "bound": 2400, "device_type": "master", "ammeter_addr": ["404887000027"] , "com6": "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028","404887000027", "404887000028"] ]
- 从上面文件可以看出,一共有四层嵌套,分别是:port、com、bound、addr数组;
- 解析方法:
-
int port_count, addr_count; int i,j; char *str; cJSON *js_root, *js_one, *js_two, *js_three, *js_four, *it, *member; str = (char *)malloc(4); js_root = load_json_file("device.json"); if(!js_root) return -1; js_one = cJSON_GetObjectItem(js_root, "port"); if(!js_one) printf("no port\n"); return -1; port_count = cJSON_GetArraySize(js_one); //获取数组中成员个数 printf("\nport count = %d\n", port_count); for(i = 0; i < port_count; i++) js_two = cJSON_GetArrayItem(js_one, i); printf("\n----------------------------------------cJSON_GetArrayItem js_one, %d ----------------------------------------\n", i); printf("%s", cJSON_Print(js_two)); switch(i) case 0: strcpy(str, "com3");break; case 1: strcpy(str, "com4");break; case 2: strcpy(str, "com5");break; case 3: strcpy(str, "com6");break; js_three = cJSON_GetObjectItem(js_two, str); printf("%s\n", cJSON_Print(js_three)); it = cJSON_GetObjectItem(js_three, "bound"); printf("name type is %s\n",it->string); printf("name is %d\n",it->valueint); it = cJSON_GetObjectItem(js_three, "device_type"); printf("name type is %s\n",it->string); printf("name is %s\n",it->valuestring); js_four = cJSON_GetObjectItem(js_three, "device_addr"); printf("%s\n", cJSON_Print(js_four)); addr_count = cJSON_GetArraySize(js_four); printf("%d\n", addr_count); if(addr_count == 0) continue; else for(j = 0; j < addr_count; j++) member = cJSON_GetArrayItem(js_four, j); printf("name is %s\n",member->valuestring);
-
运行结果:
-
port count = 4 ----------------------------------------cJSON_GetArrayItem js_one, 0 ---------------------------------------- "com3": "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028"] "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028"] name type is bound name is 2400 name type is device_type name is master ["404887000027", "404887000028"] 2 name is 404887000027 name is 404887000028 ----------------------------------------cJSON_GetArrayItem js_one, 1 ---------------------------------------- "com4": "bound": 2400, "device_type": "master", "device_addr": [] "bound": 2400, "device_type": "master", "device_addr": [] name type is bound name is 2400 name type is device_type name is DLT645_2007 [] 0 ----------------------------------------cJSON_GetArrayItem js_one, 2 ---------------------------------------- "com5": "bound": 2400, "device_type": "master", "device_addr": ["404887000027"] "bound": 2400, "device_type": "master", "device_addr": ["404887000027"] name type is bound name is 2400 name type is device_type name is master ["404887000027"] 1 name is 404887000027 ----------------------------------------cJSON_GetArrayItem js_one, 3 ---------------------------------------- "com6": "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028", "404887000027", "404887000028"] "bound": 2400, "device_type": "master", "device_addr": ["404887000027", "404887000028", "404887000027", "404887000028"] name type is bound name is 2400 name type is device_type name is master ["404887000027", "404887000028", "404887000027", "404887000028"] 4 name is 404887000027 name is 404887000028 name is 404887000027 name is 404887000028
以上是关于json文件解析的主要内容,如果未能解决你的问题,请参考以下文章