JSON
2016-06-18 15:13
351 查看
[cpp] view
plain copy
<pre name="code" class="cpp">#include <stdio.h>
#include <json/json.h>
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;
}
plain copy
<pre name="code" class="cpp">#include <stdio.h>
#include <json/json.h>
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;
}
相关文章推荐
- js图片预览
- json-c库使用总结
- Jsoncpp的使用
- (一)JavaScript基本了解
- Javascript经验总结
- json字符串转换为xml格式输出
- 正则表达式参考大全
- 反射实现对象转json格式字符串
- 从js向Action传中文参数出现乱码问题的解决方法
- Turn.js 实现翻书效果的学习与总结
- JavaScript面向对象和原型函数
- jstl中,c:set 设置的值在jsp:include 的页面失效
- js 函数
- 最简单的js 判断密码强度
- js小知识
- JavaScript 数组操作函数总结(push,pop,join,shift,unshift,slice,splice,concat)
- prototype.js常用函数详解
- Javascript中prototype的使用详解
- JavaScript 正则表达式对象RegExp test方法慎用/g
- jsp代码页面报:javax.servlet.jsp.PageContext cannot be resolved to a type错误的解决办法