Adding values to a JsonCpp object/nested JsonCpp Json:Value objects

6.1k views Asked by At

I have a JSON object say:

Json::Value temp;
temp["example1"] = "first";

which will be represented as

{
    "example1" : "first"
}

Now if I want to add another object into the above object without using the index method, how can I do it? For example:

 {
    "example1" : "first",
    "example2" : "second"
 }

but avoiding using syntax

temp["example2"] = "second";

Are there any equivalents to push_back() (like in C++ vector/list) in JsonCpp?

1

There are 1 answers

0
Sga On BEST ANSWER

The equivalent to push_back in JsonCpp is append, but you can use it only on Json::nullValue or Json::arrayValue.

That makes sense because only one parameter is needed to add an element to an array. What are you asking is unclear/not possible, because you are trying to create an object, which is like a std::map in C++, and two parameters are needed to insert an element here.