1

I want to generate json in following format and i have written code as follows as i am beginer in c++ so i want to do it in more efficient way same operation.

{
    "id": "0001",
    "type": "donut",
    "name": "cake",
    "ppu": "0.55",
    "option":
    {
        "options":
        [
            {
                "id": "5001",
                "type": "furniture"
            },
            {
                "id": "5002",
                "type": "furniture2"
            },
            {
                "id": "5003",
                "type": "furniture3"
            }
        ]
    },
    "Grid":
    [
        {
            "id": "5001",
            "type": "furniture"
        },
        {
            "id": "5002",
            "type": "furniture2"
        },
        {
            "id": "5003",
            "type": "furniture3"
        },
        {
            "id": "5004",
            "type": "furniture4"
        }
    ]
}

and i have following code for json generated

generateJson(){
boost::property_tree::ptree members,members1,child,child1,child2,child3,children,options,option;
anotherStructName c;
   members.put<string>("id","0001");
   members.put<string>("type","donut");
   members.put<string>("name","cake");
   members.put<double>("ppu",0.55);
   children.push_back(std::make_pair("",child));
   children.push_back(std::make_pair("",child1));
   children.push_back(std::make_pair("",child2));
   children.push_back(std::make_pair("",child3));
   option.push_back(std::make_pair("",child));
   option.push_back(std::make_pair("",child1));
   option.push_back(std::make_pair("",child2));
   options.put_child("option",batter);
   members.put_child("options",options);
   members.add_child("Grid",children);
 return c.createJsonString(members);
}

//now logic for create json

string anotherStructName::createJsonString(boost::property_tree::ptree json)
{
    std::stringstream jsonString;
    write_json(jsonString, json);
    return jsonString.str();
}

//above code is working fine but i want to add it through loop with using vector and data adding dynamically in "id" and "type" field of option array.

1 Answer 1

2

if you have "id", "type" datas as vectors you can generate "options" part of your json like this

vector<string> id, type;
boost::property_tree::ptree options, option;

for (int i = 0; i < id.size() && i < type.size(); ++i) {
    boost::property_tree::ptree child;

    child.put<string>("id",id[i]);
    child.put<string>("type",type[i]);
    options.push_back(std::make_pair("",child));
}

option.put_child("options",options);
5
  • ,I want to take id and type in structure.
    – Excoder
    Commented Feb 10, 2020 at 5:10
  • what is your structure?
    – idris
    Commented Feb 10, 2020 at 6:13
  • ``` struct Data{ int id;string name; }; vector<Data> data``` like this one.
    – Excoder
    Commented Feb 11, 2020 at 10:34
  • child.put<int>("id",data[i].id); child.put<string>("type",data[i].name);
    – idris
    Commented Feb 11, 2020 at 10:36
  • It is returing empty,how can i insert value of id and type,because i am here specifying only data[i].id and data[i].name.
    – Excoder
    Commented Feb 11, 2020 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.