Chinaunix首页 | 论坛 | 博客
  • 博客访问: 843618
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: C/C++

2018-06-25 14:27:40

要用到c编辑json串,看到一段代码写的不错,摘过来参考。
环境是centos7,json-c-devel-0.11-4.el7_0.x86_64
一开始编译不过去,应该是摘过来的代码太老了,有的函数不支持,改过后能编译执行。
//----------------------------------------------------//AUTHOR: lanyang123456//DATE: 2014-12-13//----------------------------------------------------json-c介绍与库下载请参考https://github.com/json-c/json-c/wiki[cpp] view plain copy


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.h>

  6. #include <json/json.h>

  7.     void test_jsonc()
  8.     {

  9.         struct json_object *infor_object = NULL;
  10.         infor_object = json_object_new_object();
  11.         if (NULL == infor_object)
  12.         {
  13.             printf("new json object failed.\n");
  14.             return;
  15.         }

  16.         struct json_object *para_object = NULL;
  17.         para_object = json_object_new_object();
  18.         if (NULL == para_object)
  19.         {
  20.             json_object_put(infor_object);//free
  21.             printf("new json object failed.\n");
  22.             return;
  23.         }

  24.         struct json_object *array_object = NULL;
  25.         array_object = json_object_new_array();
  26.         if (NULL == array_object)
  27.         {
  28.             json_object_put(infor_object);//free
  29.             json_object_put(para_object);//free
  30.             printf("new json object failed.\n");
  31.             return;
  32.         }

  33.         /*添加json值类型到数组中*/
  34.         json_object_array_add(array_object, json_object_new_int(256));
  35.         json_object_array_add(array_object, json_object_new_int(257));
  36.         json_object_array_add(array_object, json_object_new_int(258));

  37.         json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000"));
  38.         json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF"));
  39.         json_object_object_add(para_object, "Visible", json_object_new_int(1));

  40.         /*添加json名称和值到json对象集合中*/
  41.         json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo"));
  42.         json_object_object_add(infor_object, "param", para_object);
  43.         json_object_object_add(infor_object, "id", json_object_new_string("101"));

  44.         /*添加数组集合到json对象中*/
  45.         json_object_object_add(infor_object, "array", array_object);

  46.         printf("-----------json infor ---------------------------\n");
  47.         printf("%s\n", json_object_to_json_string(infor_object));
  48.         printf("-----------json infor ---------------------------\n");

  49.         struct json_object *result_object = NULL;


  50.         json_object_object_get_ex(infor_object, "method",&result_object);
  51.         printf("-----------result_object method ---------------------------\n");
  52.         printf("%s\n", json_object_to_json_string(result_object));
  53.         printf("-----------result_object method---------------------------\n");

  54.         json_object_object_get_ex(infor_object, "param",&result_object);
  55.         printf("-----------result_object param ---------------------------\n");
  56.         printf("%s\n", json_object_to_json_string(result_object));
  57.         printf("-----------result_object param---------------------------\n");

  58.         json_object_object_get_ex(infor_object, "array",&result_object);
  59.         printf("-----------result_object array---------------------------\n");
  60.         printf("%s\n", json_object_to_json_string(result_object));
  61.         printf("-----------result_object array---------------------------\n");

  62.         int i;
  63.         for(i = 0; i < json_object_array_length(result_object); i++) {
  64.           struct json_object *obj = json_object_array_get_idx(result_object, i);
  65.           printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
  66.         }

  67.         json_object_put(infor_object);//free

  68.     }


  69.     int main(int argc, char *argv[])
  70.     {
  71.         test_jsonc();

  72.         return 0;
  73.     }

点击(此处)折叠或打开

  1. # gcc -ljson-c -o main main.c
  2. [root@ca example]# ./main
  3. -----------json infor ---------------------------
  4. { "method": "GetSystemInfo", "param": { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }, "id": "101", "array": [ 256, 257, 258 ] }
  5. -----------json infor ---------------------------
  6. -----------result_object method ---------------------------
  7. "GetSystemInfo"
  8. -----------result_object method---------------------------
  9. -----------result_object param ---------------------------
  10. { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }
  11. -----------result_object param---------------------------
  12. -----------result_object array---------------------------
  13. [ 256, 257, 258 ]
  14. -----------result_object array---------------------------
  15.         [0]=256
  16.         [1]=257
  17.         [2]=258




阅读(399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~