cjson 增强对逗号处理

https://sourceforge.net/projects/cjson/

 

解析JSON比较好的轻量级工具,使用比较方便,今天测试发现有些JSON解析失败,但其他工具可以正常解析。

 

跟踪发现是cJSON对最后一个逗号分隔处理不足。譬如如下:

{"liveList":[{"name":"123",},],"code":"0",}

},]标识前面的逗号就会处理错误,理论上是空对象。

 

修改代码cJSon.c

技术分享

技术分享

 

测试代码:

int main(int argc, char * argv[])
{
     char * buff = "{\"code\":\"0\",\"isPreview\":\"0\",\"isADFreeFlag\":\"1\",\"logincode\":\"1014\",\"titleName\":\"\",\"nodeName\":\"CCTV2\",\"objType\":\"live\",\"isAudio\":\"false\",\"audioDesc\":\"??:????|nodeId=10080903\",\"playUrl\":\"http://hls.cmvideo.cn:8088/WqkB2MJM359QHiQ2ShzkitfOjzdoiTz8MF+tjo3OiT1HFopoXQNYHgnkuOCcfZeS+VKurfyXjDfwlpH2/E2GcIPjOjisQkuoQ/rua4ZnuCEbgl1aN4DvlGHKCjETOnEQ/rl3/iqecLI7ukacQKRtCb8dCBxtJ2UflZxWNfzYsvABT2DGlQenXBXeeQFMN3gifppTiH9hGcZMozYca2ECqvx0QgZGyDEbJbCTDmOr5zp9sGGBQ/0M3pP0rFOS68Dy/cctv2_49.sdp.m3u8?ec=1\",\"param\":\"contentId=60098984520150511028;nodeId=10242984;objType=live;\",\"img\":\"\",\"uc\":\"\",\"mt\":\"\",\"ucmax\":\"\",\"dlnaRate\":\"\",\"keyword\":\"\",\"livePlayback\":\"true\",\"liveList\":[{\"sTime\":\"00:01\",\"eTime\":\"00:15\",\"name\":\"?????\",},{\"sTime\":\"00:15\",\"eTime\":\"01:12\",\"name\":\"????\",},{\"sTime\":\"01:12\",\"eTime\":\"02:13\",\"name\":\"?????\",},{\"sTime\":\"02:13\",\"eTime\":\"03:11\",\"name\":\"??\",},{\"sTime\":\"03:11\",\"eTime\":\"03:54\",\"name\":\"????\",},{\"sTime\":\"03:54\",\"eTime\":\"04:36\",\"name\":\"????\",},{\"sTime\":\"04:36\",\"eTime\":\"05:07\",\"name\":\"????\",},{\"sTime\":\"05:07\",\"eTime\":\"06:10\",\"name\":\"???\",},{\"sTime\":\"06:10\",\"eTime\":\"07:00\",\"name\":\"???\",},{\"sTime\":\"07:00\",\"eTime\":\"09:00\",\"name\":\"????\",},{\"sTime\":\"09:00\",\"eTime\":\"10:05\",\"name\":\"????\",},{\"sTime\":\"10:05\",\"eTime\":\"10:40\",\"name\":\"????\",},{\"sTime\":\"10:40\",\"eTime\":\"11:10\",\"name\":\"????\",},{\"sTime\":\"11:10\",\"eTime\":\"11:28\",\"name\":\"????\",},{\"sTime\":\"11:28\",\"eTime\":\"11:50\",\"name\":\"????\",},{\"sTime\":\"11:50\",\"eTime\":\"12:30\",\"name\":\"??????\",},{\"sTime\":\"12:30\",\"eTime\":\"13:21\",\"name\":\"????\",},{\"sTime\":\"13:21\",\"eTime\":\"14:33\",\"name\":\"????\",},{\"sTime\":\"14:33\",\"eTime\":\"15:28\",\"name\":\"??????\",},{\"sTime\":\"15:28\",\"eTime\":\"16:03\",\"name\":\"?????\",},{\"sTime\":\"16:03\",\"eTime\":\"16:29\",\"name\":\"????\",},{\"sTime\":\"16:29\",\"eTime\":\"17:11\",\"name\":\"????\",},{\"sTime\":\"17:11\",\"eTime\":\"17:44\",\"name\":\"????\",},{\"sTime\":\"17:44\",\"eTime\":\"18:29\",\"name\":\"???\",},{\"sTime\":\"18:29\",\"eTime\":\"18:52\",\"name\":\"????\",},{\"sTime\":\"18:52\",\"eTime\":\"19:25\",\"name\":\"????\",},{\"sTime\":\"19:25\",\"eTime\":\"20:00\",\"name\":\"????\",},{\"sTime\":\"20:00\",\"eTime\":\"20:30\",\"name\":\"????\",},{\"sTime\":\"20:30\",\"eTime\":\"21:20\",\"name\":\"??????\",},{\"sTime\":\"21:20\",\"eTime\":\"21:56\",\"name\":\"?????\",},{\"sTime\":\"21:56\",\"eTime\":\"22:30\",\"name\":\"??????\",},{\"sTime\":\"22:30\",\"eTime\":\"23:10\",\"name\":\"??????\",},{\"sTime\":\"23:10\",\"eTime\":\"23:43\",\"name\":\"?????\",},{\"sTime\":\"23:43\",\"eTime\":\"23:59\",\"name\":\"?????\",},{\"sTime\":\"23:59\",\"eTime\":\"23:59\",\"name\":\"?????\",},],\"preparam\":\"\",\"nextparam\":\"\",\"subList\":[],\"sTime\":\"20:00\",\"eTime\":\"20:30\",\"curDay\":\"\",\"supportRate\":\"rate5=;rate4=50_8;rate3=49_8;rate2=;rate1=48_8;\",\"matchedRate\":\"rate3\",}";

        printf("################,start  json:%s   \n",buff);
                    
        cJSON *root;
        root = cJSON_Parse(buff);

        if (!root)
        {
            printf("######################!!!!!!!!!!!!!!!!!!!!!!!!!!Error before: [%s]\n",cJSON_GetErrorPtr());
            return -1;
        }
        
            printf("################end json:\n");

  return 0;
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。