JSON

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="cpp">#include <stdio.h>  
  2. #include <json/json.h>  
  3.   
  4. void test_jsonc()  
  5. {  
  6.   
  7.     struct json_object *infor_object = NULL;  
  8.     infor_object = json_object_new_object();  
  9.     if (NULL == infor_object)  
  10.     {  
  11.         printf("new json object failed.\n");  
  12.         return;  
  13.     }  
  14.   
  15.     struct json_object *para_object = NULL;  
  16.     para_object = json_object_new_object();  
  17.     if (NULL == para_object)  
  18.     {  
  19.         json_object_put(infor_object);//free  
  20.         printf("new json object failed.\n");  
  21.         return;  
  22.     }  
  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.   
  34.     /*添加json值类型到数组中*/  
  35.     json_object_array_add(array_object, json_object_new_int(256));  
  36.     json_object_array_add(array_object, json_object_new_int(257));  
  37.     json_object_array_add(array_object, json_object_new_int(258));  
  38.   
  39.     json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000"));  
  40.     json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF"));  
  41.     json_object_object_add(para_object, "Visible", json_object_new_int(1));  
  42.   
  43.     /*添加json名称和值到json对象集合中*/  
  44.     json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo"));  
  45.     json_object_object_add(infor_object, "param", para_object);  
  46.     json_object_object_add(infor_object, "id", json_object_new_string("101"));  
  47.   
  48.     /*添加数组集合到json对象中*/  
  49.     json_object_object_add(infor_object, "array", array_object);  
  50.   
  51.     printf("-----------json infor ---------------------------\n");  
  52.     printf("%s\n", json_object_to_json_string(infor_object));  
  53.     printf("-----------json infor ---------------------------\n");  
  54.   
  55.     struct json_object *result_object = NULL;  
  56.   
  57.   
  58.     result_object =  json_object_object_get(infor_object, "method");  
  59.     printf("-----------result_object method ---------------------------\n");  
  60.     printf("%s\n", json_object_to_json_string(result_object));  
  61.     printf("-----------result_object method---------------------------\n");  
  62.   
  63.     result_object =  json_object_object_get(infor_object, "param");  
  64.     printf("-----------result_object param ---------------------------\n");  
  65.     printf("%s\n", json_object_to_json_string(result_object));  
  66.     printf("-----------result_object param---------------------------\n");  
  67.   
  68.     result_object =  json_object_object_get(infor_object, "array");  
  69.     printf("-----------result_object  array---------------------------\n");  
  70.     printf("%s\n", json_object_to_json_string(result_object));  
  71.     printf("-----------result_object array---------------------------\n");  
  72.   
  73.     int i;  
  74.     for(i = 0; i < json_object_array_length(result_object); i++) {  
  75.       struct json_object *obj = json_object_array_get_idx(result_object, i);  
  76.       printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));  
  77.     }  
  78.   
  79.     json_object_put(infor_object);//free  
  80.   
  81. }  
  82.   
  83.   
  84. int main(int argc, char *argv[])  
  85. {  
  86.     test_jsonc();  
  87.   
  88.     return 0;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值