用 C 解析 JSON?
如何将 JSON 解析为 C 中的对象。我知道这对于 Ruby 这样的脚本语言来说很容易他们只需要调用一个简单的函数(例如 PHP 中的 json_decode($str)
),str
将被解析为动态对象或关联对象大批。
在 C 中做这个怎么样?有例子吗?
Possible Duplicates:
Does Windows have a JSON API that can be called from C?
Using JSON data
How to parse JSON to an object in C. I know it's easy for scripting languages like Ruby, PHP, etc. They just need to call a simple function (eg. json_decode($str)
in PHP ) and the str
will be parsed to a dynamic object or associative array.
What about doing this in C? Are there any examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Linux,json-glib 似乎是最佳选择。如果这不适合您,还有大量其他选择。
json-glib seems to be the best option if you're on Linux. There's a plethora of other choices if that doesn't work for you.
在 C 中处理 JSON 的最佳方法在很大程度上取决于您是否想要处理具有任意键、类型和层次结构的抽象、完全通用的 JSON,或者您是否正在处理具有固定值的数据。允许的字段集、这些字段的类型以及嵌套有效的实例。在后一种情况下,您最好制作与您想要接受的 JSON 相对应的 C 结构,并使用专门的代码来填充这些结构。对于完全通用的 JSON,您需要一个通用的库(包含随之而来的所有膨胀,以及每次要访问值时通过字符串键查找值而不是使用简单的
查找值的性能成本。
或->
运算符)。The best way to handle JSON in C depends a lot on whether you want to process abstract, fully general JSON with arbitrary keys, types, and hierarchy, or whether you're working with data that has a fixed set of allowed fields, types for those fields, and instances in which case nesting is valid. In the latter case, you would do well to make C structures corresponding to the JSON you want to accept, and specialized code to fill those structures. For fully general JSON, you want a general-purpose library (with all the bloat that entails, as well as the performance cost of looking up values by a string key each time you want to access them rather than with a simple
.
or->
operator).