JSON-C
来源:互联网 发布:广东联通宽带提速软件 编辑:程序博客网 时间:2024/06/06 14:59
//----------------------------------------------------//AUTHOR: lanyang123456//DATE: 2014-12-13//----------------------------------------------------json-c介绍与库下载请参考https://github.com/json-c/json-c/wiki[cpp] view plain copy
#include#include void test_jsonc() { struct json_object *infor_object = NULL; infor_object = json_object_new_object(); if (NULL == infor_object) { printf("new json object failed.\n"); return; } struct json_object *para_object = NULL; para_object = json_object_new_object(); if (NULL == para_object) { json_object_put(infor_object);//free printf("new json object failed.\n"); return; } struct json_object *array_object = NULL; array_object = json_object_new_array(); if (NULL == array_object) { json_object_put(infor_object);//free json_object_put(para_object);//free printf("new json object failed.\n"); return; } /*添加json值类型到数组中*/ json_object_array_add(array_object, json_object_new_int(256)); json_object_array_add(array_object, json_object_new_int(257)); json_object_array_add(array_object, json_object_new_int(258)); json_object_object_add(para_object, "DeviceId", json_object_new_string("sn_iso_9000")); json_object_object_add(para_object, "MacAddr", json_object_new_string("AA:BB:CC:DD:EE:FF")); json_object_object_add(para_object, "Visible", json_object_new_int(1)); /*添加json名称和值到json对象集合中*/ json_object_object_add(infor_object, "method", json_object_new_string("GetSystemInfo")); json_object_object_add(infor_object, "param", para_object); json_object_object_add(infor_object, "id", json_object_new_string("101")); /*添加数组集合到json对象中*/ json_object_object_add(infor_object, "array", array_object); printf("-----------json infor ---------------------------\n"); printf("%s\n", json_object_to_json_string(infor_object)); printf("-----------json infor ---------------------------\n"); struct json_object *result_object = NULL; result_object = json_object_object_get(infor_object, "method"); printf("-----------result_object method ---------------------------\n"); printf("%s\n", json_object_to_json_string(result_object)); printf("-----------result_object method---------------------------\n"); result_object = json_object_object_get(infor_object, "param"); printf("-----------result_object param ---------------------------\n"); printf("%s\n", json_object_to_json_string(result_object)); printf("-----------result_object param---------------------------\n"); result_object = json_object_object_get(infor_object, "array"); printf("-----------result_object array---------------------------\n"); printf("%s\n", json_object_to_json_string(result_object)); printf("-----------result_object array---------------------------\n"); int i; for(i = 0; i < json_object_array_length(result_object); i++) { struct json_object *obj = json_object_array_get_idx(result_object, i); printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); } json_object_put(infor_object);//free } int main(int argc, char *argv[]) { test_jsonc(); return 0; } 编译与执行$ gcc -o test1 test1.c -ljson$ ./test1-----------json infor ---------------------------{ "method": "GetSystemInfo", "param": { "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }, "id": "101", "array": [ 256, 257, 258 ] }-----------json infor --------------------------------------result_object method ---------------------------"GetSystemInfo"-----------result_object method--------------------------------------result_object param ---------------------------{ "DeviceId": "sn_iso_9000", "MacAddr": "AA:BB:CC:DD:EE:FF", "Visible": 1 }-----------result_object param--------------------------------------result_object array---------------------------[ 256, 257, 258 ]-----------result_object array--------------------------- [0]=256 [1]=257 [2]=258
阅读全文
0 0
- JSON json-rpc-c
- 【json】json-c接口
- C++->json
- JSON C
- json c
- JSON-C
- C#,Json解析,Json遍历
- c使用json-c库
- c使用json-c库
- c/c++使用json
- JSON in objective c
- JSON解析类(C#)
- json-c document
- objective-c-json-js
- json-c API总结
- JSON解析类(C#)
- Make JSON-C (libjson)
- [Home] JSON-C Example
- cocos2d-lua class 方法解释
- $.each 与$(selector).each()
- AngularJS的过滤器总结
- 5. scala集合(二)
- 关于svn cleanup失败
- JSON-C
- Android开发调试无法连接到夜神模拟器的解决方法
- 双11背后那些事儿
- TextView部分内容改变颜色,背景色,大小,字体,超链接,删除线,下划线,添加图片
- 解决谷歌浏览器input密码框自动填充
- PAT甲级 1048. Find Coins (25)
- Excel表格中两列数据对比,找出异同的数据
- 二分查找法
- 测试人眼中的车联网(比亚迪云服务-3.2.1)——缺少的三大功能