#include <json-c/json.h>

#include <json-c/json.h>

<json-c/json.h>JSON-C 库 的主要头文件,用于在 C 语言中 解析、生成和操作 JSON 数据。它是 Linux 系统下最常用的 JSON 处理库之一。


1. 核心功能

功能关键函数/对象用途
JSON 对象创建json_object_new_*()创建各种类型的 JSON 对象
JSON 解析json_tokener_parse()将 JSON 字符串解析为 JSON 对象
JSON 生成json_object_to_json_string()将 JSON 对象转换为 JSON 字符串
对象操作json_object_object_*()操作 JSON 对象中的键值对
数组操作json_object_array_*()操作 JSON 数组
类型检查json_object_get_type()获取 JSON 对象的类型

2. 安装 JSON-C

在 Linux 上安装:

1
2
3
4
5
# Debian/Ubuntu
sudo apt-get install libjson-c-dev

# CentOS/RHEL
sudo yum install json-c-devel

编译时链接:

1
gcc program.c -ljson-c -o program

3. 详细函数解析

3.1 JSON 对象创建

(1) 创建基本类型对象

1
2
3
4
5
// 创建不同类型的 JSON 对象
struct json_object *json_object_new_int(int i);
struct json_object *json_object_new_double(double d);
struct json_object *json_object_new_string(const char *s);
struct json_object *json_object_new_boolean(boolean b);

(2) 创建复合类型对象

1
2
3
// 创建 JSON 对象和数组
struct json_object *json_object_new_object();
struct json_object *json_object_new_array();

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <json-c/json.h>
#include <stdio.h>

int main() {
// 创建一个 JSON 对象
struct json_object *root = json_object_new_object();

// 添加键值对
json_object_object_add(root, "name", json_object_new_string("Alice"));
json_object_object_add(root, "age", json_object_new_int(25));

// 转换为 JSON 字符串并打印
printf("%s\n", json_object_to_json_string(root));

// 释放内存
json_object_put(root);
return 0;
}

输出

1
{"name":"Alice","age":25}

3.2 JSON 解析

(1) 解析 JSON 字符串

1
struct json_object *json_tokener_parse(const char *str);
  • 返回值:

    • 成功:返回 json_object
  • 失败:返回 NULL

(2) 从文件解析 JSON

1
struct json_object *json_object_from_file(const char *filename);

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <json-c/json.h>
#include <stdio.h>

int main() {
const char *json_str = "{\"city\":\"New York\",\"population\":8419000}";

// 解析 JSON 字符串
struct json_object *parsed = json_tokener_parse(json_str);

// 获取字段值
struct json_object *city, *population;
json_object_object_get_ex(parsed, "city", &city);
json_object_object_get_ex(parsed, "population", &population);

printf("City: %s\n", json_object_get_string(city));
printf("Population: %d\n", json_object_get_int(population));

json_object_put(parsed);
return 0;
}

输出

1
2
City: New York
Population: 8419000

3.3 JSON 对象操作

(1) 添加/删除键值对

1
2
void json_object_object_add(struct json_object *obj, const char *key, struct json_object *val);
void json_object_object_del(struct json_object *obj, const char *key);

(2) 获取对象值

1
json_object_object_get_ex(struct json_object *obj, const char *key, struct json_object **value);

(3) 遍历对象

1
2
3
json_object_object_foreach(obj, key, val) {
printf("Key: %s, Value: %s\n", key, json_object_to_json_string(val));
}

示例

1
2
3
4
5
6
7
8
9
10
struct json_object *root = json_object_new_object();
json_object_object_add(root, "country", json_object_new_string("Japan"));

// 删除键
json_object_object_del(root, "country");

// 遍历对象
json_object_object_foreach(root, key, val) {
printf("%s: %s\n", key, json_object_to_json_string(val));
}

3.4 JSON 数组操作

(1) 创建和操作数组

1
2
3
4
5
6
7
8
9
10
11
12
// 创建数组
struct json_object *array = json_object_new_array();

// 添加元素
json_object_array_add(array, json_object_new_int(10));
json_object_array_add(array, json_object_new_string("Hello"));

// 获取数组长度
int len = json_object_array_length(array);

// 获取指定索引的元素
struct json_object *elem = json_object_array_get_idx(array, 0);

示例

1
2
3
4
5
struct json_object *array = json_object_new_array();
json_object_array_add(array, json_object_new_int(100));
json_object_array_add(array, json_object_new_string("World"));

printf("Array: %s\n", json_object_to_json_string(array));

输出

1
[100,"World"]

3.5 类型检查

(1) 检查 JSON 对象类型

1
2
3
4
5
6
7
8
9
10
11
enum json_type {
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string
};

enum json_type json_object_get_type(const struct json_object *obj);

示例

1
2
3
4
struct json_object *obj = json_object_new_string("Test");
if (json_object_get_type(obj) == json_type_string) {
printf("It's a string!\n");
}

4. 完整示例

解析 JSON 文件并修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <json-c/json.h>
#include <stdio.h>

int main() {
// 1. 从文件解析 JSON
struct json_object *root = json_object_from_file("data.json");

// 2. 修改 JSON
json_object_object_add(root, "status", json_object_new_string("active"));

// 3. 保存回文件
json_object_to_file("updated_data.json", root);

// 4. 释放内存
json_object_put(root);
return 0;
}

5. 注意事项

  1. 内存管理:

    • 使用 json_object_put() 释放 JSON 对象(引用计数机制)。
  2. 错误处理:

    • 检查 json_tokener_parse()json_object_from_file() 的返回值。
  3. 线程安全:

    • JSON-C 不是线程安全的,多线程环境下需加锁。
  4. 性能优化:

    • 解析大型 JSON 时,考虑流式解析(如 json_tokener_new())。

6. 总结

<json-c/json.h> 的核心用途:

  1. JSON 解析:

    • json_tokener_parse(), json_object_from_file()
  2. JSON 生成:

    • json_object_to_json_string(), json_object_to_file()
  3. 对象操作:

    • json_object_object_add(), json_object_object_get_ex()
  4. 数组操作:

    • json_object_array_add(), json_object_array_get_idx()

适用于 配置文件解析、API 交互、数据存储 等场景。