acl/lib_acl_cpp/samples/json/json10/json.cpp

111 lines
3.2 KiB
C++
Raw Normal View History

#include "stdafx.h"
int main(void)
{
acl::json json;
acl::json_node& root = json.get_root();
2015-11-09 22:11:21 +08:00
// <20><><EFBFBD><EFBFBD> json <20><><EFBFBD><EFBFBD>{"1": {"1": {"1": "aa"}}}
2015-11-09 22:11:21 +08:00
root.add_child("1", true).add_child("1", true).add_text("1", "aa");
printf("%s\r\n", json.to_string().c_str());
json.reset();
//////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC> json <20><><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
2015-11-09 22:11:21 +08:00
// {"cmd": "add", "Para": {"xxx": "111", "yyy": "222", "zzz": true, "eee": 100}, "status": true, "length": 100}
//////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
root = json.get_root();
root.add_text("cmd", "add") // <20><><EFBFBD><EFBFBD> root <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ӽڵ㲢<DAB5><E3B2A2><EFBFBD><EFBFBD> root
.add_child("Para", true) // <20><><EFBFBD><EFBFBD> root <20><><EFBFBD>ӽڵ<D3BD>(Para)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Para
.add_text("xxx", "111") // <20><><EFBFBD><EFBFBD> Para <20><><EFBFBD>ӽڵ㲢<DAB5><E3B2A2><EFBFBD><EFBFBD> Para
.add_text("yyy", "222") // <20><><EFBFBD><EFBFBD> Para <20><><EFBFBD>ӽڵ㲢<DAB5><E3B2A2><EFBFBD><EFBFBD> Para
.add_bool("zzz", true) // <20><><EFBFBD><EFBFBD> Para <20><><EFBFBD>ӽڵ㲢<DAB5><E3B2A2><EFBFBD><EFBFBD> Para
.add_number("eee", 100) // <20><><EFBFBD><EFBFBD> Para <20><><EFBFBD>ӽڵ㲢<DAB5><E3B2A2><EFBFBD><EFBFBD> Para
.get_parent() // <20><><EFBFBD><EFBFBD> Para <20>ĸ<EFBFBD><C4B8>ڵ<EFBFBD>(root<6F>ڵ<EFBFBD>)
.add_bool("status", true) // <20><><EFBFBD><EFBFBD> root <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ӽڵ<D3BD> status
.add_number("length", 100); // <20><><EFBFBD><EFBFBD> root <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ӽڵ<D3BD> length
printf("%s\r\n", json.to_string().c_str());
acl::string buf1;
json.build_json(buf1);
acl::json_node* para = json.getFirstElementByTagName("Para");
if (para) {
root.add_text("root-name", "root-value");
para->get_obj()->add_text("test", "111111");
printf("%s\r\n", json.to_string().c_str());
}
//////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>ǰ<EFBFBD><C7B0>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> json <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
json.reset();
root = json.get_root();
2015-11-09 22:11:21 +08:00
root.add_text("cmd", "add")
.add_child("Para",
json.create_node()
.add_text("xxx", "111")
.add_text("yyy", "222")
.add_bool("zzz", true)
.add_number("eee", 100))
.add_bool("status", true)
.add_number("length", 100);
printf("%s\r\n", json.to_string().c_str());
acl::string buf2;
json.build_json(buf2);
//////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-11-09 22:11:21 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>ǰ<EFBFBD><C7B0>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> json <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
2015-11-09 22:11:21 +08:00
json.reset();
acl::json_node& cmd = json.create_node("cmd", "add");
root.add_child(cmd);
acl::json_node& node1 = json.create_node();
root.add_child("Para", node1);
acl::json_node& node11 = json.create_node("xxx", "111");
node1.add_child(node11);
acl::json_node& node12 = json.create_node("yyy", "222");
node1.add_child(node12);
acl::json_node& node13 = json.create_node("zzz", true);
node1.add_child(node13);
acl::json_node& node14 = json.create_node("eee", (long long int) 100);
node1.add_child(node14);
2015-11-09 22:11:21 +08:00
acl::json_node& status = json.create_node("status", true);
root.add_child(status);
acl::json_node& length = json.create_node("length", (long long int) 100);
root.add_child(length);
printf("%s\r\n", json.to_string().c_str());
2015-11-09 22:11:21 +08:00
acl::string buf3;
json.build_json(buf3);
//////////////////////////////////////////////////////////////////////
// <20>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if (buf2 == buf1 && buf3 == buf2) {
printf("OK\r\n");
} else {
printf("ERROR\r\n");
}
return 0;
}