c语言json库parson的介绍和使用
简介
- 本文章主要介绍下parson库的使用
- parson : parson是用C语言开发的一个json解析库, 提供json的序列化和反序列化功能。使用时只需要在我们的工程中引入头文件parson.h和源文件parson.c即可使用。
使用parson解析json
- 单层json结构解析
- 代码
const char* resp = "{\"code\":0, \"flag\":true, \"msg\":\"success\"}";bool bRet = false;JSON_Value *rootValue = NULL;do {//反序列化为json对象rootValue = json_parse_string(resp);if (rootValue == NULL) {break;}JSON_Object *rootObject = json_value_get_object(rootValue);if (rootObject == NULL) {break;}if (!json_object_has_value(rootObject, "code")) {break;}int code = json_object_get_number(rootObject, "code");if (!json_object_has_value(rootObject, "flag")) {break;}bool flag = json_object_get_boolean(rootObject, "flag");if (!json_object_has_value(rootObject, "msg")) {break;}const char* msg = json_object_get_string(rootObject, "msg");printf("code = %d, flag = %d, msg = %s\n", code, flag, msg);bRet = true;} while (0);if (rootValue != NULL) {json_value_free(rootValue);rootValue = NULL;}return bRet;
- 执行结果
code = 0, flag = 1, msg = success
- 多层json结构解析
- 代码
const char* resp = "{\"code\":0, \"msg\":\"success\", \"data\":{\"name\":\"lucy\", \"score\":99.5}}";bool bRet = false;JSON_Value *rootValue = NULL;do {rootValue = json_parse_string(resp);if (rootValue == NULL) {break;}JSON_Object *rootObject = json_value_get_object(rootValue);if (rootObject == NULL) {break;} if (!json_object_has_value(rootObject, "code")) {break;}int code = json_object_get_number(rootObject, "code");if (!json_object_has_value(rootObject, "msg")) {break;}const char* msg = json_object_get_string(rootObject, "msg");if (!json_object_has_value(rootObject, "data")) {break;}JSON_Object *dataObject = json_object_get_object(rootObject, "data");if (dataObject == NULL) {break;}if (!json_object_has_value(dataObject, "name")) {break;}const char* name = json_object_get_string(dataObject, "name");if (!json_object_has_value(dataObject, "score")) {break;}double score = json_object_get_number(dataObject, "score");printf("code = %d, msg = %s, name = %s, score = %lf\n", code, msg, name, score);bRet = true;} while (0);if (rootValue != NULL) {json_value_free(rootValue);rootValue = NULL;}return bRet;
- 执行结果
code = 0, msg = success, name = lucy, score = 99.500000
- 有数组的json结构解析
- 代码
const char* resp = "{\"code\":0, \"msg\":\"success\", \"result\":[\"tcp\", \"udp\", \"http\", \"tls\"]}";bool bRet = false;JSON_Value *root_value = NULL;do {root_value = json_parse_string(resp);if (root_value == NULL) {break;}JSON_Object *root_object = json_value_get_object(root_value);if (root_object == NULL) {break;}if (!json_object_has_value(root_object, "code")) {break;}int code = json_object_get_number(root_object, "code");printf("code is %d\n", code);if (!json_object_has_value(root_object, "msg")) {break;}const char* msg = json_object_get_string(root_object, "msg");printf("msg is %s\n", msg);if (!json_object_has_value(root_object, "result")) {break;}JSON_Array *result_obj = json_object_get_array(root_object, "result");if (result_obj == NULL) {break;}printf("result: ");for (int i = 0; i < json_array_get_count(result_obj); i++) {const char *pStr = json_array_get_string(result_obj, i);if (pStr != NULL) {printf("%s ", pStr);}}printf("\n");bRet = true;} while (0);if (root_value != NULL) {json_value_free(root_value);root_value = NULL;}return bRet;
- 执行结果
code is 0msg is successresult: tcp udp http tls
- json文件解析
- 文件内容 test.json
{"code":0,"flag":true,"msg":"success"}
- 代码
bool bRet = false;JSON_Value *rootValue = NULL;do {// 如果json文件内容带注释可用这个函数 json_parse_file_with_commentsrootValue = json_parse_file("test.json");if (rootValue == NULL) {break;}JSON_Object *rootObject = json_value_get_object(rootValue);if (rootObject == NULL) {break;}if (!json_object_has_value(rootObject, "code")) {break;}int code = json_object_get_number(rootObject, "code");if (!json_object_has_value(rootObject, "flag")) {break;}bool flag = json_object_get_boolean(rootObject, "flag");if (!json_object_has_value(rootObject, "msg")) {break;}const char* msg = json_object_get_string(rootObject, "msg");printf("code = %d, flag = %d, msg = %s\n", code, flag, msg);bRet = true;} while (0);if (rootValue != NULL) {json_value_free(rootValue);rootValue = NULL;}return bRet;
- 执行结果
code = 0, flag = 1, msg = success
使用parson构建json
- 单层json结构构建
- 代码
bool bRet = false;JSON_Value *root_value = NULL;do {root_value = json_value_init_object();if (root_value == NULL) {break;}JSON_Object *root_object = json_value_get_object(root_value);if (root_object == NULL) {break;}json_object_set_number(root_object, "code", 0);json_object_set_boolean(root_object, "flag", true);json_object_set_string(root_object, "msg", "success");json_object_set_number(root_object, "rate", 10.5);char *data = json_serialize_to_string(root_value);if (data == NULL) {break;}printf("%s\n", data);bRet = true;json_free_serialized_string(data);} while (0);if (root_value != NULL) {json_value_free(root_value);root_value = NULL;}return bRet;
- 执行结果
{"code":0,"flag":true,"msg":"success","rate":10.5}
- 多层json结构构建
- 代码
bool bRet = false;JSON_Value *root_value = NULL;do {root_value = json_value_init_object();if (root_value == NULL) {break;}JSON_Object *root_object = json_value_get_object(root_value);if (root_object == NULL) {break;}JSON_Value *data_value = json_value_init_object();if (data_value == NULL) {break;}JSON_Object *data_object = json_value_get_object(data_value);if (data_object == NULL) {break;}json_object_set_string(data_object, "name", "jack");json_object_set_number(data_object, "age", 28);json_object_set_number(root_object, "code", 0);json_object_set_boolean(root_object, "flag", true);json_object_set_string(root_object, "msg", "success");json_object_set_value(root_object, "data", data_value);char *data = json_serialize_to_string(root_value);if (data == NULL) {break;}printf("%s\n", data);bRet = true;json_free_serialized_string(data);} while (0);if (root_value != NULL) {json_value_free(root_value);root_value = NULL;}return bRet;
- 执行结果
{"code":0,"flag":true,"msg":"success","data":{"name":"jack","age":28}}
- 带有数组的json结构构建
- 代码
bool bRet = false;JSON_Value *root_value = NULL;do {root_value = json_value_init_object();if (root_value == NULL) {break;}JSON_Object *root_object = json_value_get_object(root_value);if (root_object == NULL) {break;}JSON_Value *data_value = json_value_init_object();if (data_value == NULL) {break;}JSON_Object *data_object = json_value_get_object(data_value);if (data_object == NULL) {break;}JSON_Value *dataValueArray = json_value_init_array();if (dataValueArray == NULL) {break;}JSON_Array *dataObjectArray = json_value_get_array(dataValueArray);if (dataObjectArray == NULL) {break;}for (int i = 0; i < 5; i++) {JSON_Value *pValue = json_value_init_object();if (pValue == NULL) {break;}JSON_Object *pObject = json_value_get_object(pValue);if (pObject == NULL) {break;}json_object_set_number(pObject, "index", i);json_object_set_string(pObject, "status","success");json_array_append_value(dataObjectArray, pValue);}json_object_set_string(data_object, "name", "jack");json_object_set_number(data_object, "age", 28);json_object_set_number(root_object, "code", 0);json_object_set_boolean(root_object, "flag", true);json_object_set_string(root_object, "msg", "success");json_object_set_value(root_object, "data", dataValueArray);char *data = json_serialize_to_string(root_value);if (data == NULL) {break;}printf("%s\n", data);bRet = true;json_free_serialized_string(data);} while (0);if (root_value != NULL) {json_value_free(root_value);root_value = NULL;}return bRet;
- 执行结果
{"code":0,"flag":true,"msg":"success","data":[{"index":0,"status":"success"},{"index":1,"status":"success"},{"index":2,"status":"success"},{"index":3,"status":"success"},{"index":4,"status":"success"}]}
- 构建json并保存到文件
- 代码
bool bRet = false;JSON_Value *root_value = NULL;do {root_value = json_value_init_object();if (root_value == NULL) {break;}JSON_Object *root_object = json_value_get_object(root_value);if (root_object == NULL) {break;}json_object_set_number(root_object, "code", 0);json_object_set_boolean(root_object, "flag", true);json_object_set_string(root_object, "msg", "success");json_object_set_number(root_object, "rate", 10.5);JSON_Status status = json_serialize_to_file(root_value, "test.json");if (status == JSONFailure) {break;}bRet = true;} while (0);if (root_value != NULL) {json_value_free(root_value);root_value = NULL;}return bRet;
- test.json文件内容
{"code":0,"flag":true,"msg":"success","rate":10.5}
c语言json库parson的介绍和使用相关推荐
- c语言JSON库 Parson的编译和使用
c语言JSON库 Parson的编译和使用 最近一直学习ndk,Android肯定会用到json,这里介绍一个c语言写成的JSON解析库 Parson介绍 Parson is a lighweight ...
- c语言json库如何编译,C语言的JSON库Jansson
Jansson概述 Jansson是一个C语言库,用于编码,解码和处理JSON数据.其主要特点和设计原则是: 简单直观的API和数据模型 全面的文档 不依赖其他库 完全支持Unicode(UTF-8) ...
- c# 对象json互相转换_Go语言进阶之路(六):内置JSON库和开源库gjson
Go语言内置了部分JSON函数,可以方便地在Go语言结构体实例和JSON字符串之间互相转换.这可比Java强多了. 不过Go语言内置的json库功能比较鸡肋,只能在结构体和JSON之间相互转换,没办法 ...
- JAVA中几种常用JSON库性能比较
点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 作者:飞污熊 xncoding.com/2018/01/09/java/jsons.html 本 ...
- c语言 json 请求_JSON的简单介绍以及C语言的JSON库使用
JSON概述 JSON: JavaScript 对象表示法( JavaScript Object Notation) .是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集. JSON采 ...
- JSON的简单介绍以及C语言的JSON库使用
本文转自:https://www.cnblogs.com/liunianshiwei/p/6087596.html JSON概述 JSON: JavaScript 对象表示法( JavaScript ...
- HandyJSON:Swift语言JSON转Model工具库
背景 JSON是移动端开发常用的应用层数据交换协议.最常见的场景便是,客户端向服务端发起网络请求,服务端返回JSON文本,然后客户端解析这个JSON文本,再把对应数据展现到页面上. 但在编程的时候,处 ...
- C++ Json库ArduinoJson介绍及使用说明
文章目录 介绍与下载 使用方法与演示 反序列化(Deserialization,解析数据) 序列化(Serialization,创建数据) API简单介绍 对象 JsonDocument JsonVa ...
- 从零开始的 Rust 语言 blas 库之预备篇(1)—— blas 基础介绍
从零开始的 Rust 语言 blas 库之预备篇(1)-- blas 基础介绍 下一篇:从零开始的 Rust 语言 blas 库之预备篇(2)-- blas 矩阵格式详解 文章部分参考:https:/ ...
最新文章
- 对神经网络某一层做了小改进,效果却提升显著,可以发论文吗?
- 新工作总结之权限系统设计
- android动画封装,Android属性动画封装,快速构建动画
- springboot 注解动态赋值_java springboot动态给注解属性参数赋值
- html如何复用其它页面,编写可以复用的 HTML 模板
- RecyclerView分割线的技巧
- ./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No suc
- 解决openfire在使用MySQL数据库后的中文乱码问题(转)
- 眼下很多创业者之所以会创业失败
- Asp.Net Mvc3.0(MEF依赖注入理论)
- USACO 4.3 Street Race(DFS)
- 匹配滤波器的MATLAB实现
- android 地址json文件,Android访问assets本地json文件的方法
- 苹果iphone5/iphone5s充电器(A1443)及电路原理图
- Could not resolve project
- c语言计算标准体重作业,c语言/* 已知成人标准体重粗算公式:
- java端口扫描器_简单端口扫描器java的设计与实现.doc
- OpenFPGA系列(四) OpenFPGA 架构描述文件和仿真配置文件
- 对于初学者的JavaScript 教程
- 郑州大学计算机硕士分数线,郑州大学今年的录取分数线是多少?不同的专业呢...
热门文章
- YTU 2901: G-险恶逃生II
- 饼图加引导线_PPT里的饼图,如何显示引导线?
- SAP AC305固定资产培训书面总结-转
- 数学分析教程史济怀练习17.1
- ZigBee和EnOcean联盟是如何将能量收集向前推进的?
- anaconda 安装第三方库
- KHD-S02 晃电再启动控制器
- android可以扫描其他app的资源,【App推荐】安卓上不为人知的冷门良心应用
- eclipse+jsp+servlet+sqlserver+tomcat实现增删改查的一个小项目(超详细教程)
- 论文参考文献排板小技巧 wordr 插件