C/C++处理JSON的开源库有名的有两个libjson与cJSON. 个人认为cJSON更好用些。


把cJSON的例子调试了下.



效果:


   

JSON(03)C/C++处理JSON_libjson


   

JSON(03)C/C++处理JSON_c++解析JSON_02


 代码文件:


/***
*desc:通过cJSON来解析
*/

#include <stdlib.h>
#include <stdio.h>
#include "cJSON.h"

void demo1()
{
char *out ;
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type", "rect");
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate", 24);

out=cJSON_Print(root);
printf("%s\n",out);
//free(out);
out=cJSON_Print(fmt);
printf("%s\n",out);
free(out);

cJSON *jName = cJSON_GetObjectItem(root,"name");
printf("name = %s \n",jName->valuestring);
}

void demo2()
{
char text4[]="{\n \"Image\": {\n \"Width\": 800,\n \"Height\": 600,\n \"Title\": \"View from 15th Floor\",\n \"Thumbnail\": {\n \"Url\": \"http:/*www.json.com/image/481989943\",\n \"Height\": 125,\n \"Width\": \"100\"\n },\n \"IDs\": [116, 943, 234, 38793]\n }\n }";
char *out;cJSON *json;

json=cJSON_Parse(text4);
if (!json)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
else
{
cJSON *pImage = cJSON_GetObjectItem(json,"Image");
printf("Image = %s \n",pImage->valuestring);

//int nCount = cJSON_GetArraySize( pImage);
//printf("nCount = %d \n",nCount);

//数组方式 0
cJSON * pArrayItem2 = cJSON_GetArrayItem(pImage, 2);
// "Title": "View from 15th Floor",
printf("2 pArrayItem2->valuestring = %s \n",pArrayItem2->valuestring);


//数组方式 1
int nCount = cJSON_GetArraySize( cJSON_GetObjectItem(pImage,"Thumbnail") );
printf("Thumbnail nCount = %d \n",nCount);

/*
"Thumbnail": {
"Url": "http:/*www.json.com/image/481989943",
*/
cJSON * pArrayItemUrl= cJSON_GetArrayItem( cJSON_GetObjectItem(pImage,"Thumbnail") , 0);
printf("Thumbnail Url = %s \n",pArrayItemUrl->valuestring);


//数组方式 2
nCount = cJSON_GetArraySize( cJSON_GetObjectItem(pImage,"IDs") );
printf("IDs nCount = %d \n",nCount);

/*
"IDs": [116, 943, 234, 38793]
*/
cJSON * pArrayItem22 = cJSON_GetArrayItem( cJSON_GetObjectItem(pImage,"IDs") , 2);
printf("22 IDs valueint 2= %d \n",pArrayItem22->valueint);

//数组方式 4
cJSON *tasklist=cJSON_GetObjectItem(pImage,"Thumbnail")->child;//子对象 //IDs
/*
"root":[{
"value":"2011-03-17 11:15 - xxxx - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id":"XXXX_20110315000"
*/
while(tasklist!=NULL)
{
//printf("%s\n",cJSON_GetObjectItem(tasklist,"value")->valuestring);
tasklist=tasklist->next;
}

//数组方式 5
nCount = cJSON_GetArraySize( pImage);
//cJSON *pArrayItem;
//for(int i= 0; i < nCount; i++)
//{
//cJSON_Print( cJSON_GetArrayItem(pImage, i));
//cJSON_Print( pArrayItem);
//}

out=cJSON_Print(json);
cJSON_Delete(json);
printf("\n ------------\n %s\n",out);
free(out);
}
}


int main(void)
{
printf("------------\n ");
printf("构建JSON\n ");
printf("------------\n \n");
demo1();

printf("\n------------\n");
printf("解析JSON\n");
printf("------------\n");
demo2();
return 0;
}