加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用cjson库解析json格式

发布时间:2020-12-16 19:07:50 所属栏目:百科 来源:网络整理
导读:配置文件cfg.ini { "status": "ok","code": 0,"devices": [ { "device_id": "dd87336a-56ba-dd88-c698-74ffba1a8886","device_name": "nliu","device_mac": "00AABBCCDD","geoip": "上海市 电信" } ]} #include stdio.h#include stdlib.h#include string.h#i

配置文件cfg.ini

{
  "status": "ok","code": 0,"devices": [
    {
      "device_id": "dd87336a-56ba-dd88-c698-74ffba1a8886","device_name": "nliu","device_mac": "00AABBCCDD","geoip": "上海市 电信"
    }
  ]
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "json.h"

int main(int argc,char **argv)
{ 
  struct json_object *new_obj;
 struct json_object *obj;
 new_obj = json_object_from_file("a");

 json_object_object_foreach(new_obj,key,val)
 { 
 if (strcmp(key,"devices") == 0)
 { 
 // printf("%sn",json_object_to_json_string(val));
 struct array_list* arr = json_object_get_array(val);
 printf("1 DEBUG %dn",json_object_array_length(val));
 struct json_object* obj = (struct json_object*)array_list_get_idx(arr,0);
 printf("%sn",json_object_get_string(obj));
 json_object_object_foreach(obj,key1,val1)
 {
 printf("key %s,:::%sn",json_object_to_json_string(val1));
 }
 }
 else
 {
 // printf("t%s: %sn",json_object_to_json_string(val));
 }
 }

 json_object_put(new_obj);
 return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读