Rapidjson: Need to write a JSON with JSON Array in it.

Created on 21 Mar 2016  Â·  13Comments  Â·  Source: Tencent/rapidjson

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.

question

Most helpful comment

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();

All 13 comments

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?

ifndef ReadJSON_h

define ReadJSON_h

include "rapidjson/writer.h"

include "rapidjson/stringbuffer.h"

include

include "rapidjson/document.h" // rapidjson's DOM-style API

include "rapidjson/prettywriter.h" // for stringify JSON

include

using namespace rapidjson;
using namespace std;

int main() {
StringBuffer s;
Writer writer(s);

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;

}

endif

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

europelee picture europelee  Â·  5Comments

Andy1978 picture Andy1978  Â·  3Comments

jimmyorourke picture jimmyorourke  Â·  3Comments

lasalvavida picture lasalvavida  Â·  5Comments

dan-ryan picture dan-ryan  Â·  6Comments