cJSON:封装与解析(二)

2023-12-07 10:38
文章标签 封装 解析 cjson

本文主要是介绍cJSON:封装与解析(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该篇介绍使用 cJSON 封装和解析数组。cJSON的简单使用请看这篇。

测试字符串:

{"name":	"children info","info":	[{"name":	"lilei","age":	5,"height":	0.45}, {"name":	"lily","age":	4,"height":	0.35}, {"name":	"hanmeimei","age":	4,"height":	0.4}]
}

1. 封装

enum json_print_formate
{PRINT_FORMATE = 0,    /*按 json 格式输出*/PRINT_UNFORMATE,      /*以字符串形式输出*/
};char *json_array_package(enum json_print_formate flag)
{cJSON *root = NULL; cJSON *info = NULL;cJSON *child_info = NULL;char *out = NULL;root = cJSON_CreateObject(); /*创建 json 根节点*/cJSON_AddStringToObject(root, "name", "children info");  /*添加字符串*/info = cJSON_CreateArray(); /*创建 json 数组*/cJSON_AddItemToObject(root, "info", info);    /*将数组加入到根节点*/child_info = cJSON_CreateObject();    /*向数组中添加成员*/cJSON_AddItemToArray(info, child_info);cJSON_AddStringToObject(child_info, "name", "lilei");  /*添加字符串*/cJSON_AddNumberToObject(child_info, "age", 5);      /*添加整型*/cJSON_AddNumberToObject(child_info, "height", 0.45);  /*添加浮点型*/child_info = cJSON_CreateObject();    /*向数组中添加成员*/cJSON_AddItemToArray(info, child_info);cJSON_AddStringToObject(child_info, "name", "lily");  /*添加字符串*/cJSON_AddNumberToObject(child_info, "age", 4);      /*添加整型*/cJSON_AddNumberToObject(child_info, "height", 0.35);  /*添加浮点型*/child_info = cJSON_CreateObject();    /*向数组中添加成员*/cJSON_AddItemToArray(info, child_info);cJSON_AddStringToObject(child_info, "name", "hanmeimei");  /*添加字符串*/cJSON_AddNumberToObject(child_info, "age", 4);      /*添加整型*/cJSON_AddNumberToObject(child_info, "height", 0.40);  /*添加浮点型*/if(PRINT_FORMATE == flag){out = cJSON_Print(root);   }else{out = cJSON_PrintUnformatted(root);    /*将json形式打印成正常字符串形式*/}/*释放内存*/  cJSON_Delete(root);     return out;  
}

2. 解析

void json_array_parse(char *json_string)
{cJSON *root = NULL;    /*json 根节点*/cJSON *name = NULL;cJSON *jarray = NULL;    /*json 数组*/cJSON *member = NULL;cJSON *age = NULL;cJSON *height = NULL;root = cJSON_Parse(json_string);name = cJSON_GetObjectItem( root , "name" );printf("root name:%s\n", name->valuestring);jarray = cJSON_GetObjectItem(root, "info");cJSON_ArrayForEach(member, jarray)    /*遍历数组*/{name = cJSON_GetObjectItem( member , "name" );age = cJSON_GetObjectItem( member , "age" );height = cJSON_GetObjectItem( member , "height" );printf("name:%s, age:%d, height:%f\n", name->valuestring, age->valueint, height->valuedouble);}/*释放内存*/  cJSON_Delete(root);     
}

3. 测试

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"int main()
{char *json_str = NULL;json_str = json_array_package(PRINT_FORMATE);printf("array formate:\n%s\n", json_str);json_array_parse(json_str);cJSON_free(json_str);json_str = json_array_package(PRINT_UNFORMATE);printf("\narray unformate:\n%s\n", json_str);json_array_parse(json_str);cJSON_free(json_str);}

   CMakeLists.txt

cmake_minimum_required(VERSION 3.5)project(json_test)include_directories(./)aux_source_directory(./ SRC_FILES)add_executable(${PROJECT_NAME} ${SRC_FILES})

4. 结果

array formate:
{"name":	"children info","info":	[{"name":	"lilei","age":	5,"height":	0.45}, {"name":	"lily","age":	4,"height":	0.35}, {"name":	"hanmeimei","age":	4,"height":	0.4}]
}
root name:children info
name:lilei, age:5, height:0.450000
name:lily, age:4, height:0.350000
name:hanmeimei, age:4, height:0.400000array unformate:
{"name":"children info","info":[{"name":"lilei","age":5,"height":0.45},{"name":"lily","age":4,"height":0.35},{"name":"hanmeimei","age":4,"height":0.4}]}
root name:children info
name:lilei, age:5, height:0.450000
name:lily, age:4, height:0.350000
name:hanmeimei, age:4, height:0.400000

Note:

cJSON_Print(const cJSON *item) 和 cJSON_PrintUnformatted(const cJSON *item) 这两个函数会调用 malloc 分配内存,需要调用 cJSON_free(void *object) 进行释放。

上一篇

这篇关于cJSON:封装与解析(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/465545

相关文章

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷