Ubuntu中 json 打包数据的使用
1.JSON的概念和作用
为了避免不同平台下的字节对齐、类型大小不统一的问题,json库把数据封装成具有一定格式的字符流数据,进行传输。
json格式:把数据与键值一一对应,数据传输双方约定好同一键值,
使用接口API根据键值操作json对象(json_object)存储或取得数据。
一般使用:
数据--> (封装)json对象--> String格式 --> ...传输。。。-->String格式-->(解析)json对象-->取得数据
(int/char..)数据,与键值成对存入json对象---------->通过键值从json对象取得数据
2.JSON的安装
在 Ubuntu 中,可以使用 APT 包管理器 在线安装 JSON 解析库。不同的编程语言和应用场景可能需要不同的 JSON 库,这里解释的是一些常见的 JSON 解析工具和库的安装方法。
2.1. 离线安装
1》将安装包移动到家目录中
json-c-0.9.tar.gz(在网上直接查询,有很多这样的下载安装包,也可以后台私信直接找我要)
下载安装包路径:
wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz
2》执行解压命令
sudo tar -xvf json-c-0.9.tar.gz
3》移动到解压出来的目录中
cd json-c-0.9/
4》执行命令
sudo ./configure
5》执行命令
sudo make
6》执行命令
sudo make install
7》验证安装是否成功
只能通过编写程序去验证
头文件包含#include <json/json.h>
编译需要链接库-ljson
2.2. 使用apt 安装 jq(命令行 JSON 解析工具)
jq 是一个 轻量级 JSON 处理工具,用于 格式化、过滤、操作 JSON 数据。
🔹 安装 jq
sudo apt update
sudo apt install jq
🔹 测试 jq
echo '{"name": "Alice", "age": 25}' | jq .
🔹 输出
{
"name": "Alice",
"age": 25
}
2.3. 安装 libjson-c(C 语言 JSON 解析库)
如果需要在 C 语言 中解析 JSON,可以安装 json-c
库。
🔹 安装 libjson-c
sudo apt update
sudo apt install libjson-c-dev
🔹 检查安装
pkg-config --modversion json-c
🔹 编译 C 代码(使用 -ljson-c
进行链接)
gcc my_json_program.c -o my_json_program -ljson-c
2.4. 安装 python3-json(Python 解析 JSON)
由于 Python 自带 json 模块,可以安装 python3-json 以扩展功能。
🔹 安装 python3-json
sudo apt install python3-json
🔹 在 Python 代码中使用
import json
data = json.loads('{"name": "Alice", "age": 25}')
print(data["name"]) # 输出: Alice
2.5. 安装 rapidjson(C++ 高性能 JSON 解析库)
RapidJSON 是一个 高效的 C++ JSON 库,适用于高性能应用。
🔹 安装 rapidjson
sudo apt install rapidjson-dev
🔹 使用示例
#include <iostream>
#include <rapidjson/document.h>
int main() {
const char* json = "{\"name\": \"Alice\", \"age\": 25}";
rapidjson::Document doc;
doc.Parse(json);
std::cout << "Name: " << doc["name"].GetString() << std::endl;
return 0;
}
🔹 编译
g++ my_json_program.cpp -o my_json_program -std=c++11
2.6. 安装 json-glib(GNOME JSON 解析库)
如果需要在 GTK/GNOME 应用 中解析 JSON,可以使用 json-glib
。
🔹 安装 json-glib
sudo apt install libjson-glib-dev
🔹 检查安装
pkg-config --modversion json-glib-1.0
2.7. 安装 node-json(Node.js JSON 解析)
如果使用 Node.js 处理 JSON,可以安装 node-json 模块。
🔹 安装 Node.js
sudo apt install nodejs npm
🔹 安装 json CLI 工具
npm install -g json
🔹 使用示例
echo '{"name": "Alice", "age": 25}' | json name
📌 输出
Alice
3.JSON的接口函数
(
#include <json/json.h>
注意:在json中所有数据类型(arry、int、string、char)都是一个json对象
)
1》数据的封装(单对象(int、char、string)和数组(arry))
(1)新建对象:
A.创建一个Json对象:
struct json_object * json_object_new_object (void)
B.创建一个Json数组对象:
struct json_object * json_object_new_array (void)
C.销毁json对象
void json_object_put (struct json_object *obj)
(2)json对象的转换(普通类型->json对象):
1:struct json_object * json_object_new_int (int i)
2:struct json_object * json_object_new_double (double d)
3:struct json_object * json_object_new_string (const char *s)
4:struct json_object * json_object_new_boolean (boolean b)
5:struct json_object * json_object_new_string_len (const char *s, int len)
(3)json对象的处理
A.普通对象
添加:void json_object_object_add (struct json_object *obj, const char *key, struct json_object *val)
//@obj --- 大对象 @key --- 键(取货码) @val --- 小对象
删除:void json_object_object_del (struct json_object *obj, const char *key)
查询:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
B.数组对象
获取长度:int json_object_array_length (struct json_object *obj)
添加:int json_object_array_add (struct json_object *obj, struct json_object *val)
指定位置添加:int json_object_array_put_idx (struct json_object *obj, int idx, struct json_object *val)
获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)
(4)json_object To 字符流
const char * json_object_to_json_string (struct json_object *obj)
2》数据的解析(解析获取到的json格式字符流)
(1)字符流 To json_object
struct json_object* json_tokener_parse(const char *str)
(2)对象获取
A.普通对象
根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
//@obj --- 大对象 @key --- 键(取货码)
B.数组对象
获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)
(3)对象的转换(数据还原)
bool型:boolean json_object_get_boolean (struct json_object *obj)
double型:double json_object_get_double (struct json_object *obj)
整型:int json_object_get_int (struct json_object *obj)
字符数组:const char * json_object_get_string (struct json_object *obj)
json封装数据代码
/**JSON封装数据的步骤**/
1》将普通数据类型转换为JSON小对象类型
2》创建出JSON大对象
3》将小对象贴上标签,传入到大对象中
4》将整合好的大对象转换为字符串
/************************************************
程序中需包含该头文件#include <json/json.h>
编译时需链接对应的库 -ljson
************************************************/
/**JSON解析数据的步骤**/
1》将字符串转换为JSON大对象
2》根据标签从JSON大对象中提取出JSON小对象
3》将JSON小对象转换为普通数据类型
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <json/json.h>
struct user{
int ID;
char name[20];
char pwd[20];
};
int main(void)
{
/*JSON封装数据*/
//准备好原始数据
struct user send_user = {1,"Tonly","123456"};
//将普通数据类型转换为JSON小对象类型
struct json_object * obj_ID = json_object_new_int (send_user.ID);
struct json_object * obj_name = json_object_new_string (send_user.name);
struct json_object * obj_pwd = json_object_new_string (send_user.pwd);
//创建出JSON大对象
struct json_object * object = json_object_new_object ();
//将小对象贴上标签,传入到大对象中
json_object_object_add (object, "ID_JSON", obj_ID);
json_object_object_add (object, "name_JSON", obj_name);
json_object_object_add (object, "pwd_JSON", obj_pwd);
//将整合好的大对象转换为字符串
const char * str = json_object_to_json_string (object);
printf("JSON封装后的结果为:\n");
printf("%s\n",str);
printf("==============================================\n");
/*JSON解析数据*/
struct user recv_user;
//将字符串转换为JSON大对象
struct json_object* objects = json_tokener_parse(str);
//根据标签从JSON大对象中提取出JSON小对象
struct json_object * objs_ID = json_object_object_get (objects, "ID_JSON");
struct json_object * objs_name = json_object_object_get (objects, "name_JSON");
struct json_object * objs_pwd = json_object_object_get (objects, "pwd_JSON");
//将JSON小对象转换为普通数据类型
recv_user.ID = json_object_get_int (objs_ID);
strcpy(recv_user.name, json_object_get_string (objs_name));
strcpy(recv_user.pwd, json_object_get_string (objs_pwd));
printf("JSON解析后的结果为:\n%d,%s,%s",recv_user.ID,recv_user.name,recv_user.pwd);
return 0;
}
出现报错:
#问题:
farsight@Ubuntu18:~/shared/Network/day05/json$ ./json
./json: error while loading shared libraries: libjson.so.0: cannot open shared object file: No such file or directory
#解决办法:
1》打开配置文件
sudo vim /etc/ld.so.conf.d/my.conf
2》在新一行加上以下路径
/usr/lib(你的json安装在哪个文件夹就配置地址在哪)
3》使配置文件生效
sudo ldconfig
/**运行结果**/
farsight@Ubuntu18:~/shared/Network/day05/json$ ./json
JSON封装后的结果为:
{ "ID_JSON": 1, "name_JSON": "Tonly", "pwd_JSON": "123456" }
以上。仅供学习与分享交流,请勿用于商业用途!转载需提前说明。
我是一个十分热爱技术的程序员,希望这篇文章能够对您有帮助,也希望认识更多热爱程序开发的小伙伴。
感谢!