首页 > Web开发 > 详细

在VS2013下编译json-c库,并简单生成json格式数据

时间:2017-08-30 00:43:45      阅读:866      评论:0      收藏:0      [点我收藏+]

#include "stdafx.h"
#include "json-c/json.h"

int _tmain(int argc, _TCHAR* argv[])
{
// 正常的json格式
json_object *json = json_object_new_object();
json_object_object_add(json, "name", json_object_new_string("laomeng"));
json_object_object_add(json, "email", json_object_new_string("laomeng188@163.com"));
json_object_object_add(json, "age", json_object_new_int(30));
// 产生一个json数组格式
json_object *tech = json_object_new_array();
json_object_array_add(tech, json_object_new_string("c"));
json_object_array_add(tech, json_object_new_string("c++"));
json_object_array_add(tech, json_object_new_string("php"));
json_object_object_add(json, "technology", tech);
// 数据中包含正常json格式
json_object *tech2 = json_object_new_array();
json_object *json_sub = json_object_new_object();
json_object_object_add(json_sub, "name", json_object_new_string("laomeng"));
json_object_object_add(json_sub, "email", json_object_new_string("laomeng188@163.com"));
json_object_object_add(json_sub, "age", json_object_new_int(30));
json_object_array_add(tech2, json_sub);
json_object_object_add(json, "technology2", tech2);
// 输出
const char *str = json_object_to_json_string(json);
/*
{ "name": "laomeng", "email": "laomeng188@163.com", "age": 30, "technology": [ "c", "c++", "php" ], "technology2": [ { "name": "laomeng", "email": "laomeng188@163.com", "age": 30 } ] }
*/

printf("%s\n", str);

json_object_put(json);
return 0;
}

在VS2013下编译json-c库,并简单生成json格式数据

原文:http://www.cnblogs.com/Floki/p/7450924.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!