void json_print_array(json_object *obj) { if(!obj) return; int length=json_object_array_length(obj); for(int i=0;i<length;i++) { json_object *val=json_object_array_get_idx(obj,i); json_print_value(val); } }
void json_print_object (json_object * obj) { if (!obj) return; json_object_object_foreach (obj, key, val) { printf ("%s => ", key); json_print_value (val); }
/* *打印JSON object类型的值 *返回类型: none *返回值: none *传入参数json_object *类型 *author mleaf_hexi *mail:[email protected] */ static void json_print_object(json_object *obj) { char *configData="configData"; char *requestId="requestId"; char *delim=":/,";//分割字符串 char *p; char *s; char *name,*value,*next,*value1,*next1,*next2; int i; char buff[50],buff2[50]; if(!obj) return; //遍历json对象的key和值 //Linux内核2.6.29,说明了strtok()已经不再使用,由速度更快的strsep()代替。 json_object_object_foreach(obj,key,val) { //printf("%s => ",key); if(strcmp(key,configData)==0)//取出通讯用url { printf("configData checking out\n"); printf("configData=%s\n",json_object_get_string(val)); s=(char*)json_object_get_string(val); printf("%s\n",s); //wss: //area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0,wss: //primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0 value = strdup(s); for(i=0 ;i<2 ;i++) { // 第一次执行时 name = strsep(&value,":"); // 以":"分割字符串,这时strsep函数返回值为 "wss",即":"号之前的字符串 next =value; // 这时指针value指向":"号后面的字符串,即 //area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0,wss: //primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0 printf(" name= %s\n",name); //打印出一轮分割后name的值 name = strsep(&value,"/");// 这时通过"/"分割字符串 next =value; name = strsep(&value,"/");//去掉第二个/ next =value; if(i==0) { value=strsep(&next,",");// 以","分割字符串,这时strsep函数返回值为 "area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0",即","号之前的字符串 printf("value= %s\n",value); next2 =value; value=strsep(&next2,"/"); printf("value= %s\n",value);//area1.dts.mpush.brxy-cloud.com sprintf(buff2,"/%s",next2); printf("next2= %s\n",buff2);/* /websocket/connHandler/v2.0 */ } if(i==1)//第二轮循环 { value1=strsep(&next,"");// 以/0分割字符串,这时strsep函数返回值为 "primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0",即wss: //之后"/0"之前的字符串 printf("value1= %s\n",value1); next1 =value1; value1=strsep(&next1,"/"); printf("value2= %s\n",value1);//primary.dts.mpush.brxy-cloud.com sprintf(buff,"/%s",next1); printf("next1= %s\n",buff);/* /websocket/connHandler/v2.0 */ } value=next; } // char *source = strdup(s); // char *token; // for(token = strsep(&source, delim); token != NULL; token = strsep(&source, delim)) // { // printf("%s",token); // printf("\n"); // } } else if (strcmp(key,requestId) == 0)//取出requestId 号 { array_list* arr = json_object_get_array(val); json_object* obj = (json_object*)array_list_get_idx(arr,0); printf("requestId checking out\n"); printf("requestId=%s\n", json_object_get_string(obj)); } json_print_value(val); } }