json-c有时我们会忽略的问题

本文对比了几种常见的JSON解析库,并详细介绍了选择json-c的原因及其使用过程中遇到的问题和解决办法。作者分享了如何避免程序因JSON对象不符合标准格式而崩溃的经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        现在JSON解析库满天飞,没有必要再自己写个了,除非是商业程序  。我试过JsonCPP, QJson, Json-Spirit, jaula等C++的库。遇到总总问题,有不支持中文(UTF-8编码也不行)、不方便在Windows下编译、体积过于臃肿等毛病,最终的选择是C库 json-c,因为Google看到一篇文章对JSON的各个C库的优点比较,说Json-C兼容性最好,而且支持中文(使用UTF-8)。

        最初的目的,系统间传递数据用,如结构体转变成json就可以在系统间传递,避免了诸如边界对齐,字长字短,大头小头等等问题。有时人们使用XML,XML太复杂了,处理起来性能较差。json在大多数情况下可以取代XML从而提高系统性能。

        就json来说,由于结构比较简单,不用库也是可以的。但json-c提供了超出json范围的一些功能,实际上完成了数据序列化和反序列化,数据的存储和检索,数据对象化等功能。还是非常有使用价值的。缺点是鲁棒性还有缺陷,异常使用容易崩溃。
需要对它进行一些修改,增加参数合理性检查什么的。

错误处理:

      在linux下编译开源的json-c-0.9库,在json对象不符合标准格式时,引发segmentation fault问题,程序退出。没有任何响应,这实在是让人无语。 经发现与json_tokener_parse() 失败有很大关系。

1. 错误的情况
这里先复习一下json提取子对象的一般过程,先parse,再get object,

 json_object *newobj=NULL;
 newobj = json_tokener_parse(mystr);
 if(NULL==newobj) //错误
 {
     printf("json parse err!");
     break;
 }
 json_object *sub_obj=json_object_object_get(newobj, "name333");//因为name333不存在,所以出错! segmetation fault了,程序在这里直接崩溃


错在哪里呢?原来是在NULL==newobj这里。libjson这个库对parse错误的判断方法是is_error()宏,

#define is_error(ptr) ((unsigned long)ptr > (unsigned long)-4000L)
不得不抱怨一下这奇怪的判断方法,但是它就是这么规定的。

2. 正确的情况:
再写一个正确的:
 
const char *mystr="{\"studentid\":\"123\", \"name\":\"Jacky\", \"info\":[{\"age\":18},{\"sex\":\"male\"}]}";
new_obj = json_tokener_parse(mystr);
if( is_error(new_obj) ) //正确
{
    printf("oh, my god. json_tokener_parse err\n");
}
else
{
    printf("object parse okay\n");

    json_object *sub1obj = json_object_object_get(new_obj, "name");
    if(NULL==sub1obj) //这里就要用NULL判断了, 得不到对应的object就是NULL
    {
        printf("sub1obj err\n");
    }
    else
    {
        printf("sub1obj:%s\n", json_object_to_json_string(sub1obj));
        json_object_put(sub1obj);
    }
    //----------------
   
    json_object *sub2obj = json_object_object_get(new_obj, "info");
    if(NULL==sub2obj)
    {
        printf("sub2obj err\n");
    }
    else
    {
        printf("sub2obj:%s\n", json_object_to_json_string(sub2obj));
        json_object_put(new_obj);
    }
}//else end


原文转载自:http://zengriguang.blog.163.com/blog/static/17076248720121080187635/

npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: ues-tiny@0.0.0 npm WARN Found: rxjs@6.4.0 npm WARN node_modules/rxjs npm WARN rxjs@"^6.5.3" from the root project npm WARN 4 more (@ant-design/icons-angular, @ngx-translate/core, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer rxjs@"~6.5.3" from ng-zorro-antd@9.3.0 npm WARN node_modules/ng-zorro-antd npm WARN ng-zorro-antd@"^9.3.0" from the root project npm WARN 1 more (@nsfocus-ues/ues-common) npm WARN ERESOLVE overriding peer dependency npm WARN While resolving: ues-tiny@0.0.0 npm WARN Found: rxjs@6.4.0 npm WARN node_modules/rxjs npm WARN rxjs@"^6.5.3" from the root project npm WARN 4 more (@ant-design/icons-angular, @ngx-translate/core, ...) npm WARN npm WARN Could not resolve dependency: npm WARN peer rxjs@"~6.5.3" from ng-zorro-antd@9.3.0 npm WARN node_modules/ng-zorro-antd npm WARN ng-zorro-antd@"^9.3.0" from the root project npm WARN 1 more (@nsfocus-ues/ues-common) npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: ngx-json-viewer@3.0.2 npm ERR! Found: @angular/common@9.1.13 npm ERR! node_modules/@angular/common npm ERR! @angular/common@"^9.1.13" from the root project npm ERR! peer @angular/common@"^9.0.0 || ^10.0.0-0" from @angular/cdk@9.2.4 npm ERR! node_modules/@angular/cdk npm ERR! @angular/cdk@"^9.0.0" from ng-zorro-antd@9.3.0 npm ERR! node_modules/ng-zorro-antd npm ERR! ng-zorro-antd@"^9.3.0" from the root project npm ERR! 1 more (@nsfocus-ues/ues-common) npm ERR! 11 more (@angular/forms, @angular/platform-browser, ...) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer @angular/common@">=10.0.0" from ngx-json-viewer@3.0.2 npm ERR! node_modules/ngx-json-viewer npm ERR! ngx-json-viewer@"~3.0.2" from the root project npm ERR! npm ERR! Conflicting peer dependency: @angular/common@20.0.6 npm ERR! node_modules/@angular/common npm ERR! peer @angular/common@">=10.0.0" from ngx-json-viewer@3.0.2 npm ERR! node_modules/ngx-json-viewer npm ERR! ngx-json-viewer@"~3.0.2" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See C:\nvm\nvm\node_cache\eresolve-report.txt for a full report. npm ERR! A complete log of this run can be found in: npm ERR! C:\nvm\nvm\node_cache\_logs\2025-07-07T10_52_50_583Z-debug-0.log
最新发布
07-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值