Hi,
I could not find a way of creating a JSON that has an array of JSON.
The structure I am trying to make is as below:
{"hello":"world",
"t":true,
"f":false,
"i":123,
"pi":3.1416,
"array":[{"param_1":"ABC","param_2":"DEF"},
{"param_1":"ABC","param_2":"DEF"}]
}
Please suggest me a way using RapidJSON to create something like this.
Hi @arunmlvtec ,
What errors did you get?
A writer's StartObject function can be nested in StartArray to create your JSON object.
Following tutorial can be helpful: https://github.com/miloyip/rapidjson/blob/master/example/simplewriter/simplewriter.cpp
If you have had a JSON object(as aValue) already, you can push back it into the array.
Look at following section in our manual:
http://rapidjson.org/md_doc_tutorial.html#ModifyArray
Hi @thebusytypist ,
I referred to the simplewriter.cpp only when I started writing a JSON. However, it doesn't sort the problem I am facing here. I want to make an array of JSON structure as below:
"arrayOfJSON":[{"param_a":"ABC","param_b":"DEF"},{"param_a":"abc","param_b":"def"},]
I cannot find a way to create this in C++. I can easily do this in PHP and believe it is doable in C++ also, but don't know how.. :(
Looks like the second link might help me. Do you have any example code that uses "modifying object" scenario?
http://rapidjson.org/md_doc_tutorial.html#ModifyObject
This shows how to add or remove members from an object.
@thebusytypist Thanks.. I will try this out.
@thebusytypist I tried using from the above link.
The problems I faced are:
‘kObject’ was not declared in this scope"
I don't know what "kOject" should be?
‘document’ was not declared in this scope
In my requirement, I don't have a document. I just need to write a JSON with the given parameter.
@arunmlvtec
I do not remember this error message.
Could you provide a sample of your code?
using namespace rapidjson;
using namespace std;
int main() {
StringBuffer s;
Writer
writer.StartObject(); // Between StartObject()/EndObject(),
writer.Key("hello"); // output a key,
writer.String("world"); // follow by a value.
writer.Key("t");
writer.Bool(true);
writer.Key("f");
writer.Bool(false);
writer.Key("n");
writer.Null();
writer.Key("i");
writer.Uint(123);
writer.Key("pi");
writer.Double(3.1416);
writer.Key("a");
Value contact(kObject);
contact.AddMember("name", "Milo", document.GetAllocator());
contact.AddMember("married", true, document.GetAllocator());
writer.EndObject();
cout << s.GetString() << endl;
return 0;
}
I do not see where is kObject is defined. You should declare it before using.
And I think it is not a RapidJSON related question.
I would suggest you to ask/search for answers on Google/StackOverflow for more detailed explanation.
There are two issues in your code.
You mixed Writer (SAX style API) and Value (DOM style API).
RapidJSON declared kObjectType, not kObject.
To write an object within an object by Writer, you can simply:
writer.StartObject();
// ...
writer.Key("person");
writer.StartObject(); // the value of key "person" is an object
writer.Key("name");
writer.String("Milo");
writer.Key("married");
writer.Bool(true);
writer.EndObject(); // end of the object under key "person"
writer.EndObject();
It was so easy after looking the above example, it just didn't get into my mind... Thanks @miloyip
I have to create an array of nested JSON as well.
An example is:
{"a":[{"ABC":"abc","DEF":"def"},
{"ABC":"xyz","DEF":"pqr"},
{"ABC":"mn","DEF":"op"},]
}
Is this going to be like the same as above?
Yes. The only difference between object and array is that array does not need Key().
Hum, certainly a typo, but it's the reverse : an object doesn't need a key.
Most helpful comment
There are two issues in your code.
You mixed
Writer(SAX style API) andValue(DOM style API).RapidJSON declared
kObjectType, notkObject.To write an object within an object by
Writer, you can simply: