Linux JSON解析对象实例

json解析对象:a.json
{
 "name": "中国",
 "cities": [{
   "省份": "江苏",
   "城市": ["南京", "苏州"],
   "邮编": 123
  }, {
   "省份": "广东",
   "城市": ["深圳", "广州"],
   "邮编": 123
  }, {
   "省份": "湖北",
   "城市": ["武汉", "宜昌"],
   "邮编": 123
  }]
}
```

#include 
#include "cJSON.h"
#include 

void cJSON_();
 
int main()
{
	cJSON* obj=cJSON_CreateObject();
	cJSON_AddStringToObject(obj,"name","中国");
        
	cJSON* arr=cJSON_CreateArray();	
		
	cJSON* obj1=cJSON_CreateObject();    //江苏的
	cJSON_AddStringToObject(obj1,"省份","江苏");
	cJSON* arr1=cJSON_CreateArray();
	cJSON* item=cJSON_CreateString("南京");
	cJSON* item1=cJSON_CreateString("苏州");
	cJSON_AddItemToArray(arr1,item);
        cJSON_AddItemToArray(arr1,item1);
        cJSON_AddItemToObject(obj1,"城市",arr1);
	cJSON_AddNumberToObject(obj1,"邮编",123);

	cJSON* obj2=cJSON_CreateObject();    //广东的
	cJSON_AddStringToObject(obj2,"省份","广东");
	cJSON* arr2=cJSON_CreateArray();
	cJSON* item2=cJSON_CreateString("深圳");
	cJSON* item3=cJSON_CreateString("广州");
	cJSON_AddItemToArray(arr2,item2);
        cJSON_AddItemToArray(arr2,item3);
        cJSON_AddItemToObject(obj2,"城市",arr2);
	cJSON_AddNumberToObject(obj2,"邮编",123); 

	cJSON* obj3=cJSON_CreateObject();    //湖北的
	cJSON_AddStringToObject(obj3,"省份","湖北");
	cJSON* arr3=cJSON_CreateArray();
	cJSON* item4=cJSON_CreateString("武汉");
	cJSON* item5=cJSON_CreateString("宜昌");
	cJSON_AddItemToArray(arr3,item4);
        cJSON_AddItemToArray(arr3,item5);
        cJSON_AddItemToObject(obj3,"城市",arr3);
	cJSON_AddNumberToObject(obj3,"邮编",123);

        cJSON_AddItemToArray(arr,obj1);
        cJSON_AddItemToArray(arr,obj2);
        cJSON_AddItemToArray(arr,obj3);
        
        cJSON_AddItemToObject(obj,"cities",arr);

	char* p=cJSON_Print(obj);
	FILE* fp=fopen("a.json","w");
	fwrite(p,strlen(p),1,fp);
	fclose(fp);
		 
	//-------------------------------------------
 
	fp=fopen("a.json","rb");
	fseek(fp,0,SEEK_END);
	int len=ftell(fp);
	rewind(fp);
	char a[len+1];
	fread(a,len,1,fp);
	a[len]='\0';
	const char* j=a;
	cJSON* json=cJSON_Parse(j);
	cJSON_(json);

	return 0;

}

void cJSON_(cJSON* json)
{
	int i;
	int size=0;
        int type;
	while(json)
	{
	     type=json->type;			
             switch(type)
	     {
		case 0:{
			  printf ("false");
			  break;
			}
		case 1:{
			   printf ("true");
			   break;
			}
		case 2:{
			    printf ("null");
			    break;
                        }
		case 3:{
			     printf ("%d",json->valueint);
			     break;
			}
		case 4:{
			     printf ("\"%s\"",json->valuestring);
			     break;
			}
		case 5:{   
                            printf  ("[");
			    cJSON* item=json->child;						   
                            if (item !=NULL)
				   cJSON_(item);
			    if(json->next)
				    printf (",");
			    printf("]\n");
			    break;
			}
		case 6:{
			     printf ("{");
			     cJSON* obj=json->child;
			     while(obj)
			     {
				  printf ("\"%s\":",obj->string);
				  cJSON_(obj);
				  if(obj->next)
				        printf (",");
				   obj=obj->next;
			      }
			      printf ("}\n");
			       break;
			  }
		}
		json=json->next;
		if(json)
		    printf (",");
	}

}

 

你可能感兴趣的:(数据类型学习)