源字符串
{
"menu": {
"header": "SVG Viewer",
"items": [{
"id": "Open"
}, {
"id": "OpenNew",
"label": "Open New"
}, null, {
"id": "ZoomIn",
"label": "Zoom In"
}, {
"id": "ZoomOut",
"label": "Zoom Out"
}, {
"id": "OriginalView",
"label": "Original View"
}, null, {
"id": "Quality"
}, {
"id": "Pause"
}, {
"id": "Mute"
}, null, {
"id": "Find",
"label": "Find..."
}, {
"id": "FindAgain",
"label": "Find Again"
}, {
"id": "Copy"
}, {
"id": "CopyAgain",
"label": "Copy Again"
}, {
"id": "CopySVG",
"label": "Copy SVG"
}, {
"id": "ViewSVG",
"label": "View SVG"
}, {
"id": "ViewSource",
"label": "View Source"
}, {
"id": "SaveAs",
"label": "Save As"
}, null, {
"id": "Help"
}, {
"id": "About",
"label": "About Adobe CVG Viewer..."
}]
}
}
//先构建cJSON对象
CJSON *root=CJSON_CreateObject();
CJSON *menu=CJSON_AddObjectToObject(root,"menu");
CJSON_AddStringToObject(menu,"header","SVG Viewer");
CJSON *items=CJSON_AddArrayToObject(menu,"items");
CJSON *element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Open");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","OpenNew");
CJSON_AddStringToObject(element,"label","Open New");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateNull();
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","ZoomIn");
CJSON_AddStringToObject(element,"label","Zoom In");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","ZoomOut");
CJSON_AddStringToObject(element,"label","Zoom Out");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","OriginalView");
CJSON_AddStringToObject(element,"label","Original View");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateNull();
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Quality");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Pause");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Mute");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateNull();
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Find");
CJSON_AddStringToObject(element,"label","Find...");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","FindAgain");
CJSON_AddStringToObject(element,"label","Find Again");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Copy");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","CopyAgain");
CJSON_AddStringToObject(element,"label","Copy Again");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","CopySVG");
CJSON_AddStringToObject(element,"label","Copy SVG");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","ViewSVG");
CJSON_AddStringToObject(element,"label","View SVG");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","ViewSource");
CJSON_AddStringToObject(element,"label","View Source");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","SaveAs");
CJSON_AddStringToObject(element,"label","Save As");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateNull();
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","Help");
CJSON_AddItemToArray(items,element);
element=CJSON_CreateObject();
CJSON_AddStringToObject(element,"id","About");
CJSON_AddStringToObject(element,"label","About Adobe CVG Viewer...");
CJSON_AddItemToArray(items,element);
char *cjson_string=CJSON_Print(root);
CJSON *source=CJSON_Parse(cjson_string);
printf("%s\n",CJSON_Print(source));
CJSON_Delete(source);
// FILE *fp=fopen("test.json","w+");
// fprintf(fp,"%s",CJSON_Print(source));
// fclose(fp);
//解析
//提取二级目录下的header信息
CJSON *tmp=CJSON_GetObjectItemCaseSensitive(menu,"header");
if(CJSON_IsString(tmp))printf("%s\n",tmp->valuestring);
//提取数组项,并遍历输出,id label信息
tmp=CJSON_GetObjectItemCaseSensitive(menu,"items");
CJSON *child=NULL;
if(tmp)
{
printf("%s \n",tmp->string);
child=tmp->child;
}
while(child)
{
printf("-------\n");
if(CJSON_IsNull(child))printf("NULL!\n");
else
{
tmp=child->child;
while(tmp)
{
if(CJSON_IsString(tmp))
{
printf("%s :%s \n",tmp->string,tmp->valuestring);
}
else
{
printf("false fomat!");
}
tmp=tmp->next;
}
}
child=child->next;
}
控制台输出:
{
"menu": {
"header": "SVG Viewer",
"items": [{
"id": "Open"
}, {
"id": "OpenNew",
"label": "Open New"
}, null, {
"id": "ZoomIn",
"label": "Zoom In"
}, {
"id": "ZoomOut",
"label": "Zoom Out"
}, {
"id": "OriginalView",
"label": "Original View"
}, null, {
"id": "Quality"
}, {
"id": "Pause"
}, {
"id": "Mute"
}, null, {
"id": "Find",
"label": "Find..."
}, {
"id": "FindAgain",
"label": "Find Again"
}, {
"id": "Copy"
}, {
"id": "CopyAgain",
"label": "Copy Again"
}, {
"id": "CopySVG",
"label": "Copy SVG"
}, {
"id": "ViewSVG",
"label": "View SVG"
}, {
"id": "ViewSource",
"label": "View Source"
}, {
"id": "SaveAs",
"label": "Save As"
}, null, {
"id": "Help"
}, {
"id": "About",
"label": "About Adobe CVG Viewer..."
}]
}
}
SVG Viewer
items
-------
id :Open
-------
id :OpenNew
label :Open New
-------
NULL!
-------
id :ZoomIn
label :Zoom In
-------
id :ZoomOut
label :Zoom Out
-------
id :OriginalView
label :Original View
-------
NULL!
-------
id :Quality
-------
id :Pause
-------
id :Mute
-------
NULL!
-------
id :Find
label :Find...
-------
id :FindAgain
label :Find Again
-------
id :Copy
-------
id :CopyAgain
label :Copy Again
-------
id :CopySVG
label :Copy SVG
-------
id :ViewSVG
label :View SVG
-------
id :ViewSource
label :View Source
-------
id :SaveAs
label :Save As
-------
NULL!
-------
id :Help
-------
id :About
label :About Adobe CVG Viewer...