json - cJSON c++ - add item object -
i'm using cjson library (https://github.com/davegamble/cjson). body example request json this:
{ "user": { "name":"user name", "city":"user city" } }
i add objects , work:
cjson *root; cjson *user; root = cjson_createobject(); cjson_additemtoobject(root,"user", user = cjson_createobject()); cjson_addstringtoobject(user, "name", name.c_str()); cjson_addstringtoobject(user, "city", city.c_str());
but have body json little different:
{ "user": { "informations:"{ "name1":"user name1", "name2":"user name 2" } } }
and try add object this:
cjson *root; cjson *user; cjson *info; root = cjson_createobject(); cjson_additemtoobject(root,"user", user = cjson_createobject()); cjson_additemtoobject(user,"informations", info = cjson_createobject()); cjson_addstringtoobject(info, "name", name.c_str()); cjson_addstringtoobject(info, "city", city.c_str());
its right way using cjson? because not workin , dont know if problem in c++ or in java client send data c++ server.
thanks lot.
although did not specify, why code not working, code below, should generate sample provided.
#include <iostream> #include "cjson.h" int main() { cjson *root; cjson *user; cjson *info; std::string name1 = "user name1"; std::string name2 = "user name 2"; root = cjson_createobject(); cjson_additemtoobject(root,"user", user = cjson_createobject()); cjson_additemtoobject(user,"informations", info = cjson_createobject()); cjson_addstringtoobject(info, "name1", name1.c_str()); cjson_addstringtoobject(info, "name2", name2.c_str()); std::cout << cjson_print(root) << std::endl; return 0; }
the cjson documentation seems pretty straightforward , code looks fine. there "test.c" file in cjson sources, can find more code samples how use it.
Comments
Post a Comment