#include <stdio.h>#include <stdlib.h>#include "cJSON.h"void printJson(cJSON * root)//以递归的方式打印json的最内层键值对{ ???for(int i=0; i<cJSON_GetArraySize(root); i++) ??//遍历最外层json键值对 ???{ ???????cJSON * item = cJSON_GetArrayItem(root, i); ???????????????if(cJSON_Object == item->type) ?????//如果对应键的值仍为cJSON_Object就递归调用printJson ???????????printJson(item); ???????else ???????????????????????????????//值不为json对象就直接打印出键和值 ???????{ ???????????printf("%s->", item->string); ???????????printf("%s\n", cJSON_Print(item)); ???????} ???}}int main(){ ???char * jsonStr = "{\"semantic\":{\"slots\":{\"name\":\"张三\"}}, \"rc\":0, \"operation\":\"CALL\", \"service\":\"telephone\", \"text\":\"打电话给张三\"}"; ???cJSON * root = NULL; ???cJSON * item = NULL;//cjson对象 ???root = cJSON_Parse(jsonStr); ????????if (!root) ????{ ???????printf("Error before: [%s]\n",cJSON_GetErrorPtr()); ???} ???else ???{ ???????printf("%s\n", "有格式的方式打印Json:"); ??????????????????printf("%s\n\n", cJSON_Print(root)); ???????printf("%s\n", "无格式方式打印json:"); ???????printf("%s\n\n", cJSON_PrintUnformatted(root)); ???????printf("%s\n", "一步一步的获取name 键值对:"); ???????printf("%s\n", "获取semantic下的cjson对象:"); ???????item = cJSON_GetObjectItem(root, "semantic");// ???????printf("%s\n", cJSON_Print(item)); ???????printf("%s\n", "获取slots下的cjson对象"); ???????item = cJSON_GetObjectItem(item, "slots"); ???????printf("%s\n", cJSON_Print(item)); ???????printf("%s\n", "获取name下的cjson对象"); ???????item = cJSON_GetObjectItem(item, "name"); ???????printf("%s\n", cJSON_Print(item)); ???????printf("%s:", item->string); ??//看一下cjson对象的结构体中这两个成员的意思 ???????printf("%s\n", item->valuestring); ???????????????????????????????printf("\n%s\n", "打印json所有最内层键值对:"); ???????printJson(root); ???} ???return 0; ???}
CJSON ?parse.c
原文地址:https://www.cnblogs.com/liyubo/p/9091155.html