代码拉取完成,页面将自动刷新
C++ 版本 json 库 https://gitee.com/linuxmail/lib-zc/blob/master/doc/json_cpp.md
LIB-ZC 支持 JSON, 其 STRUCT 类型是 zjson_t
struct zjson_t {
union {
zbool_t b;
long l;
double d;
zbuf_t *s;
zvector_t *v; /* <zjson_t *> */
zmap_t *m; /* <char *, zjson_t *> */
} val;
zjson_t *parent;
unsigned char type;
};
下面演示通过API逐步生成一个 json
{
"description": "this is json demo, describe json'apis.",
"author": "http://linuxmail.cn",
"thanks": ["you", "he", 123, true, 2.01, null],
"APIS": {
"constructor": [ "json()", "json(std::string &str)", "json(bool val)" ],
"array_add_element": ["给数组添加一个子节点", "add a new zcc::json element", "nothing"],
"object_add_element": "给对象添加一个子节点"
},
"score": 0.98,
"version": 12,
"published": false
}
zjson_t *json = zjson_create();
zjson_t *tmpj;
tmpj = zjson_create_string("this is json demo, describe json'apis.", -1);
zjson_object_update(json, "description", tmpj, 0);
得到:
{
"description": "this is json demo, describe json'apis."
}
tmpj = zjson_object_update(json, "author", zjson_create_string("http://linuxmail.cn/", -1), 0);
得到
{
"description": "this is json demo, describe json'apis.",
"author": "http://linuxmail.cn/"
}
tmpj = zjson_object_update(json, "thanks", zjson_create(), 0);
zjson_array_push(tmpj, zjson_create_string("you", -1));
zjson_array_push(tmpj, zjson_create_string("he", -1));
zjson_array_push(tmpj, zjson_create_long(123));
zjson_array_push(tmpj, zjson_create_bool(1));
zjson_array_push(tmpj, zjson_create_double(2.01));
zjson_array_push(tmpj, zjson_create_null());
得到
{
"description": "this is json demo, describe json'apis.",
"author": "http://linuxmail.cn/",
"thanks": ["you", "he", 123, true, 2.01, null]
}
tmpj = zjson_object_update(json, "APIS", zjson_create(), 0);
tmpj = zjson_object_update(tmpj, "constructor", zjson_create(), 0);
zjson_array_push(tmpj, zjson_create_string("json()", -1));
zjson_array_push(tmpj, zjson_create_string("json(std::string &str)", -1));
zjson_array_push(tmpj, zjson_create_string("json(bool val)", -1));
tmpj = zjson_object_update(tmpj, "array_add_element", zjson_create(), 0);
zjson_array_push(tmpj, zjson_create_string("给数组添加一个子节点", -1));
zjson_array_push(tmpj, zjson_create_string("add a new zcc::json element", -1));
zjson_array_push(tmpj, zjson_create_string("nothing", -1));
tmpj = zjson_object_update(tmpj, "object_add_element", zjson_create_string("给对象添加一个子节点", -1), 0);
得到:
{
"description": "this is json demo, describe json'apis.",
"author": "http://linuxmail.cn/",
"thanks": ["you", "he", 123, true, 2.01, null],
"APIS": { "constructor": [ "json()", "json(std::string &str)", "json(bool val)" ],
"array_add_element": ["给数组添加一个子节点", "add a new zcc::json element", "nothing"],
"object_add_element": "给对象添加一个子节点"
}
}
zjson_object_update(json, "score", zjson_create_double(0.98), 0);
zjson_object_update(json, "version", zjson_create_long(12), 0);
zjson_object_update(json, "published", zjson_create_string(".........", -1), 0);
zjson_object_update(json, "published", zjson_create_bool(0), 0);
得到最终目标
{
"description": "this is json demo, describe json'apis.",
"author": "http://linuxmail.cn",
"thanks": ["you", "he", 123, true, 2.01, null],
"APIS": {
"constructor": [ "json()", "json(std::string &str)", "json(bool val)" ],
"array_add_element": ["给数组添加一个子节点", "add a new zcc::json element", "nothing"],
"object_add_element": "给对象添加一个子节点"
},
"score": 0.98,
"version": 12,
"published": false
}
zjson_free(json);
下面的操作, 如果不是对象类型, 则首先转换为值为 {} 的对象类型
下面操作, 如果不是数组,先转为值为 [] 的数组
zjson_t *j = zjson_create();
const char *s = "{\"errcode\": \"-801\", \"errmsg\": \"Domain Not Exist\"}\r\n";
zjson_unserialize(j, s, -1);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。