标签:wan ber 解析 ons san count service 序列化 AMM
{ "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
{ "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"] } } ] }
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
标签:wan ber 解析 ons san count service 序列化 AMM
原文地址:https://www.cnblogs.com/icefree/p/11273471.html