使用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;
}

相关文章

文章浏览阅读2.4k次。最近要优化cesium里的热力图效果,浏览...
文章浏览阅读1.2w次,点赞3次,收藏19次。在 Python中读取 j...
文章浏览阅读1.4k次。首字母缩略词 API 代表应用程序编程接口...
文章浏览阅读802次,点赞10次,收藏10次。解决一个JSON反序列...
文章浏览阅读882次。Unity Json和Xml的序列化和反序列化_uni...