当前位置: 首页 > news >正文

CentOS 7 使用cJSON 库

什么是JSON

JSON是一种轻量级的数据交换格式,可读性强、编写简单。键值对组合编写规则,键名使用双引号包裹,冒号:分隔符后面紧跟着数值,有两种常用的数据类型是对象和数组。

对象:使用花括号{}包裹起来的内容,数据结构{“key1”: “value1”, “key2”:“value2” …},key为对象的属性,value为对象的值。

数值:使用中括号[]包裹起来的内容,数据结构{“key”: [“value1”, “value2”, “value3” …]}。
 

CentOS 7 安装cJSON 库

cJSON Github 地址:https://github.com/DaveGamble/cJSON

步骤1:首先,你需要下载cJSON的源代码。你可以从https://github.com/DaveGamble/cJSON或者源代码官方网站下载。并上传至/usr/local/source_code/  

步骤2:下载完成后,需要将源代码解压,可以使用以下命令: 

[root@localhost source_code]# tar -zxvf cJSON-1.7.15.tar.gz

 步骤3:解压后,切换到源代码目录: 

[root@localhost source_code]# cd cJSON-1.7.15

步骤4:生成cJSON动态/静态库,执行如下指令: 

[root@localhost cJSON-1.7.15]# mkdir build
[root@localhost cJSON-1.7.15]# cd build/
[root@localhost build]# cmake ..
CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required):Compatibility with CMake < 2.8.12 will be removed from a future version ofCMake.Update the VERSION argument <min> value or use a ...<max> suffix to tellCMake that the project does not need compatibility with older versions.-- The C compiler identification is GNU 8.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
******
[root@localhost build]# make && make install
[  2%] Building C object CMakeFiles/cjson.dir/cJSON.c.o
[  4%] Linking C shared library libcjson.so
[  4%] Built target cjson
[  6%] Building C object CMakeFiles/cJSON_test.dir/test.c.o
[  8%] Linking C executable cJSON_test
[  8%] Built target cJSON_test
[ 11%] Building C object tests/CMakeFiles/unity.dir/unity/src/unity.c.o
[ 13%] Linking C static library libunity.a
[ 13%] Built target unity
[ 15%] Building C object tests/CMakeFiles/print_number.dir/print_number.c.o
[ 17%] Linking C executable print_number
-- 省略--
[100%] Built target fuzz_main
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include/cjson/cJSON.h
-- Installing: /usr/local/lib64/pkgconfig/libcjson.pc
-- Installing: /usr/local/lib64/libcjson.so.1.7.15
-- Installing: /usr/local/lib64/libcjson.so.1
-- Installing: /usr/local/lib64/libcjson.so
-- Installing: /usr/local/lib64/cmake/cJSON/cjson.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cjson-noconfig.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cJSONConfig.cmake
-- Installing: /usr/local/lib64/cmake/cJSON/cJSONConfigVersion.cmake

遇到的问题及解决办法

编译执行./test_cjson,提示如下截图错误信息:

[root@localhost cJSON_demo]# ./test_cjson
./test_cjson: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory

从报错的原因上看,本机上没有找到cJOSN类库的静态库/动态库libcjson.so。

首先检查/usr/local/lib 和/usr/local/include 目录中是否包含cjson 静态或动态库,可以执行如下指令:

ls /usr/local/lib | grep cjson
ls /usr/local/include | grep cjson

从上面分组 查询结果可知:cjson 没有在/usr/local/lib 库中生成cjson 静态/动态库链接。

再次查看cJOSN 在执行make && make install 指令时,对应cJSON 静态/动态库生成链接存放目录地址。

从上述截图可知cJSON 静态/动态库被安装到了/usr/local/lib64 目录中。

将/usr/local/lib64 目录添加至本机静态/动态库链接目录文件中,执行如下指令:

[root@localhost cJSON_demo]# cat /etc/ld.so.conf.d/usr-libs.conf
/usr/local/lib
[root@localhost cJSON_demo]# vi /etc/ld.so.conf.d/usr-libs.conf
[root@localhost ~]#  cat /etc/ld.so.conf.d/usr-libs.conf
/usr/local/lib
/usr/local/lib64
[root@localhost cJSON_demo]# sudo ldconfig

再次编译执行./test_cjson, Main 函数正确输出。

cJSON快速入门

在/usr/local/source_code 新增 cJSON_demo目录并新增test_cjson.c 文件,文件内容如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>int main()
{cJSON *json = NULL;cJSON *node = NULL;cJSON *tnode = NULL;cJSON *tnode2 = NULL;char *json_data = NULL;int i, j, size, size2;char *data = "{\"serialNumber\":\"212089842348362300\", \\"cellularInfo\":\[\{\"name\":\"ethernet0/0/1\",\\"switch\":\"0\"},\{\"name\":\"ethernet0/0/2\",\\"switch\":\"1\"},\{\"name\":\"ethernet0/0/3\",\\"switch\":\"0\"}\],\\"family\":[\"father\",\"mother\",\"brother\",\"sister\",\"somebody\"]\}";json = cJSON_Parse(data);json_data = cJSON_Print(json);printf("data: %s\n", json_data);free(json_data);node = cJSON_GetObjectItem(json,"serialNumber");if(node == NULL)printf("serialNumber: no\n");elseprintf("serialNumber: ok\n");node = cJSON_GetObjectItem(json, "family");if (node == NULL)printf("family: no\n");elseprintf("family: ok\n");if (node->type == cJSON_Array){printf("family array size is %d\n", cJSON_GetArraySize(node));size = cJSON_GetArraySize(node);for (i=0; i<size; i++){tnode = cJSON_GetArrayItem(node, i);if (tnode->type == cJSON_String)printf("%d: %s\n", i, tnode->valuestring);elseprintf("node type is not string, value = %d\n", tnode->type);}}node = cJSON_GetObjectItem(json, "cellularInfo");if(node == NULL)printf("cellularInfo: no\n");elseprintf("cellularInfo: ok\n");if (node->type == cJSON_Array){printf("cellularInfo array size is %d\n", cJSON_GetArraySize(node));size = cJSON_GetArraySize(node);for (i=0; i<size; i++){tnode = cJSON_GetArrayItem(node, i);if (tnode->type == cJSON_String)printf("%d: %s\n", i, tnode->valuestring);else if (tnode->type == cJSON_Object){size2 = cJSON_GetArraySize(tnode);for (j=0; j<size2; j++){tnode2 = cJSON_GetArrayItem(tnode, j);if (tnode2->type == cJSON_String)printf("%d-%d: %s\n", i, j, tnode2->valuestring);elseprintf("tnod2 type is err\n");}}elseprintf("node type is not string, value = %d\n", tnode->type);}}cJSON_Delete(json);return 0;
}

编译源码并执行:

[root@localhost cJSON_demo]# gcc -o test_cjson test_cjson.c -lcjson
[root@localhost cJSON_demo]# ./test_cjson
data: {"serialNumber": "212089842348362300","cellularInfo": [{"name": "ethernet0/0/1","switch":       "0"}, {"name": "ethernet0/0/2","switch":       "1"}, {"name": "ethernet0/0/3","switch":       "0"}],"family":       ["father", "mother", "brother", "sister", "somebody"]
}
serialNumber: ok
family: ok
family array size is 5
0: father
1: mother
2: brother
3: sister
4: somebody
cellularInfo: ok
cellularInfo array size is 3
0-0: ethernet0/0/1
0-1: 0
1-0: ethernet0/0/2
1-1: 1
2-0: ethernet0/0/3
2-1: 0

cJSON 参考资料

cJSON Github 官网地址:https://github.com/DaveGamble/cJSON

cJSON 开发参考: https://zhuanlan.zhihu.com/p/55095477?utm_oi=892471685738024960

相关文章:

  • Navicat 技术指引 | 适用于 GaussDB 的模型功能
  • C# APS.NET CORE 6.0 WEB API IIS部署
  • C语言之内存函数
  • 第十二章 : Spring Boot 日志框架详解
  • 云原生Kubernetes系列 | Kubernetes静态Pod的使用
  • 基本数据结构二叉树(1)
  • qml ParticleSystem3D使用介绍
  • 初始化与反初始化
  • Linux学习教程(第八章 Linux用户和用户组管理)三
  • 在ASP.NET Core 中使用 .NET Aspire 消息传递组件
  • rabbitMq确认机制之ConfirmType
  • 数据结构与算法【B树】的Java实现+图解
  • maven常用打包命令,值传递和引用传递,Java包 ,JDK 中常用的包有哪些,import java和javax有什么区别
  • 鸿蒙(HarmonyOS)应用开发——ArkTs学习准备
  • 中南大学2021级云计算复习笔记
  • 自己简单写的 事件订阅机制
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Apache Pulsar 2.1 重磅发布
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • Java 内存分配及垃圾回收机制初探
  • Javascript Math对象和Date对象常用方法详解
  • linux安装openssl、swoole等扩展的具体步骤
  • Python_OOP
  • Python学习笔记 字符串拼接
  • select2 取值 遍历 设置默认值
  • Theano - 导数
  • Vue ES6 Jade Scss Webpack Gulp
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 前端面试题总结
  • 如何实现 font-size 的响应式
  • Linux权限管理(week1_day5)--技术流ken
  • ​用户画像从0到100的构建思路
  • # 计算机视觉入门
  • (4)(4.6) Triducer
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • (转)fock函数详解
  • (转)Linux整合apache和tomcat构建Web服务器
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .net下的富文本编辑器FCKeditor的配置方法
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka
  • @ModelAttribute使用详解