C语言 使用 C 解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6673936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON using C
提问by dshipper
I'm trying to find a good way to parse JSON in C. I really don't need a huge library or anything, I would rather have something small and lightweight with a bare minimum of features, but good documentation.
我正在尝试找到一种在 C 中解析 JSON 的好方法。我真的不需要一个庞大的库或任何东西,我宁愿有一些小而轻的东西,功能最少,但文档很好。
Does anyone have anything they can point me to?
有没有人可以指点我?
采纳答案by Merlyn Morgan-Graham
Json isn't a huge language to start with, so libraries for it are likely to be small(er than Xml libraries, at least).
Json 并不是一门庞大的语言,因此它的库可能很小(至少比 Xml 库小)。
There a whole ton of C libraries linked at Json.org. Maybe one of them will work well for you.
在 Json.org 上链接了大量的C 库。也许其中之一对你有用。
回答by NateS
cJSONhas a decent API and is small (2 files, ~700 lines). Many of the other JSON parsers I looked at first were huge... I just want to parse some JSON.
cJSON有一个不错的 API 并且很小(2 个文件,约 700 行)。我最初看到的许多其他 JSON 解析器都很庞大……我只想解析一些 JSON。
Edit: We've made some improvementsto cJSON over the years.
编辑:多年来,我们对 cJSON 进行了一些改进。
回答by Prabhpreet
Jsmn is quite minimalistic and has only two functions to work with.
Jsmn 非常简约,只有两个功能可以使用。
回答by Yaroslav Stavnichiy
NXJSONis full-featured yet very small (~400 lines of code) JSON parser, which has easy to use API:
NXJSON是功能齐全但非常小(约 400 行代码)的 JSON 解析器,它具有易于使用的 API:
const nx_json* json=nx_json_parse_utf8(code);
printf("hello=%s\n", nx_json_get(json, "hello")->text_value);
const nx_json* arr=nx_json_get(json, "my-array");
int i;
for (i=0; i<arr->length; i++) {
const nx_json* item=nx_json_item(arr, i);
printf("arr[%d]=(%d) %ld\n", i, (int)item->type, item->int_value);
}
nx_json_free(json);
回答by TantrajJa
You can have a look at Jansson
你可以看看Jansson
The website states the following: Jansson is a C library for encoding, decoding and manipulating JSON data. It features:
该网站声明如下: Jansson 是一个用于编码、解码和操作 JSON 数据的 C 库。它的特点是:
- Simple and intuitive API and data model
- Can both encode to and decode from JSON
- Comprehensive documentation
- No dependencies on other libraries
- Full Unicode support (UTF-8)
- Extensive test suite
- 简单直观的 API 和数据模型
- 既可以编码为 JSON,也可以从 JSON 解码
- 综合文档
- 不依赖其他库
- 完全 Unicode 支持 (UTF-8)
- 广泛的测试套件
回答by Alex Reynolds
I used JSON-Cfor a work project and would recommend it. Lightweight and is released with open licensing.
我在一个工作项目中使用了JSON-C,并会推荐它。轻量级并以开放许可方式发布。
Documentation is included in the distribution. You basically have *_addfunctions to create JSON objects, equivalent *_putfunctions to release their memory, and utility functions that convert types and output objects in string representation.
文档包含在分发中。您基本上拥有*_add创建 JSON 对象的*_put函数、释放其内存的等效函数以及转换类型和以字符串表示形式输出对象的实用函数。
The licensing allows inclusion with your project. We used it in this way, compiling JSON-C as a static library that is linked in with the main build. That way, we don't have to worry about dependencies (other than installing Xcode).
许可允许包含在您的项目中。我们以这种方式使用它,将 JSON-C 编译为与主构建链接的静态库。这样,我们就不必担心依赖项(除了安装 Xcode)。
JSON-C also built for us under OS X (x86 Intel) and Linux (x86 Intel) without incident. If your project needs to be portable, this is a good start.
JSON-C 也在 OS X(x86 Intel)和 Linux(x86 Intel)下为我们构建,没有发生任何事故。如果您的项目需要可移植,这是一个好的开始。
回答by R.. GitHub STOP HELPING ICE
Do you need to parse arbitrary JSON structures, or just data that's specific to your application. If the latter, you can make it a lot lighter and more efficient by not having to generate any hash table/map structure mapping JSON keys to values; you can instead just store the data directly into struct fields or whatever.
您是否需要解析任意 JSON 结构,或者只是特定于您的应用程序的数据。如果是后者,您可以通过无需生成任何将 JSON 键映射到值的哈希表/映射结构来使其更轻巧、更高效;您可以将数据直接存储到结构字段或其他任何内容中。

