分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

CJSON ?parse.c

发布时间:2023-09-06 01:56责任编辑:董明明关键词:暂无标签
#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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved