Hi,

This is what you want:

#include <iostream>
#include <cxxtools/json.h>

int main()
{
      cxxtools::SerializationInfo si;
      si.addMember("success") <<= true;

      cxxtools::SerializationInfo& data = si.addMember("data");
      data.setCategory(cxxtools::SerializationInfo::Array);

      cxxtools::SerializationInfo& datamember = data.addMember("");

      datamember.addMember("id") <<= 5;
      datamember.addMember("account") <<= "user1";
      datamember.addMember("group_id") <<= 1;

      cxxtools::SerializationInfo& group = datamember.addMember("group");
      group.addMember("id") <<= 1;
      group.addMember("name") <<= "Administrators";

      cxxtools::JsonSerializer j;
      j.beautify(true);
      j.plainkey(true);

      j.begin(std::cout)
       .serialize(si)
       .finish();
}

I changed some other things also. It is always helpful to give variables 
good names. s1 or s2 is so nothing.

I still suggest to put that into a serialization operator in a object. 
You may well create a object, which has some optional attributes.


Tommi

Am 24.06.2015 um 23:06 schrieb Jean-Marc Choulet:
> Hello,
>
> I want to generate this JSON structure :
>
> {
>     success: true,
>     data: [
>       {
>         id: 5,
>         account: "user1",
>         group_id: 1,
>         group: {
>           id: 1,
>           name: "Administrators"
>         }
>       }
>     ]
> }
>
> I must use cxxtools::SerializationInfo because sometimes, I need to add
> dynamically some field to the JSON structure. My code:
>
> #include <iostream>
> #include <sstream>
> #include <vector>
> #include <cxxtools/jsonserializer.h>
>
> int main()
> {
>       std::ostringstream o;
>       cxxtools::JsonSerializer j(o);
>       cxxtools::SerializationInfo s1, s2;
>       std::vector<cxxtools::SerializationInfo> data;
>
>       s1.addMember("success") <<= true;
>
>       s1.addMember("id") <<= 5;
>       s1.addMember("account") <<= "user1";
>       s1.addMember("group_id") <<= 1;
>
>       s2.addMember("id") <<= 1;
>       s2.addMember("name") <<= "Administrators";
>
>       s1.addMember("group") <<= s2;
>
>       data.push_back(s1);
>
>       j.serialize(true, "success")
>        .serialize(data, "data")
>        .finish();
>
>       std::cout << o.str() << std::endl;
>
>       return 0;
> }
>
> The result :
>
> {"success":true,"data":[{"success":true,"id":5,"account":"user1","group_id":1,{"id":1,"name":"Administrators"}}]}
>
> The JSON structure is invalid. I made a error somewhere ?
>
> Thanks,
>
> Jean-Marc
>
>
> ------------------------------------------------------------------------------
> Monitor 25 network devices or servers for free with OpManager!
> OpManager is web-based network management software that monitors
> network devices and physical & virtual servers, alerts via email & sms
> for fault. Monitor 25 devices for free with no restriction. Download now
> http://ad.doubleclick.net/ddm/clk/292181274;119417398;o
> _______________________________________________
> Tntnet-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/tntnet-general


------------------------------------------------------------------------------
Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors 
network devices and physical & virtual servers, alerts via email & sms 
for fault. Monitor 25 devices for free with no restriction. Download now
http://ad.doubleclick.net/ddm/clk/292181274;119417398;o
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to