使用cjson库解析json格式

前端之家收集整理的这篇文章主要介绍了使用cjson库解析json格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

配置文件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("%s\n",json_object_to_json_string(val));
 struct array_list* arr = json_object_get_array(val);
 printf("1 DEBUG %d\n",json_object_array_length(val));
 struct json_object* obj = (struct json_object*)array_list_get_idx(arr,0);
 printf("%s\n",json_object_get_string(obj));
 json_object_object_foreach(obj,key1,val1)
 {
 printf("key %s,:::%s\n",json_object_to_json_string(val1));
 }
 }
 else
 {
 // printf("\t%s: %s\n",json_object_to_json_string(val));
 }
 }

 json_object_put(new_obj);
 return 0;
}

猜你在找的Json相关文章