Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a nested object inside a JSON string using rapidjson

I need to retrieve a nested object inside a JSON string and I'm trying to do it using rapidjson. All I've found is how to retrieve arrays and basic types, but not sub-objects. I have created the following toy example which gives an error:

rapidjson::Document document;
std::string test =  " { \"a\": { \"z\" : 21 } } ";
std::cout << test << std::endl;
if ( document.Parse<0>( test.c_str() ).HasParseError() ) {
    std::cout << "Parsing error" << std::endl;
} else {
    if ( document[ "a" ].IsObject() ) {
        std::cout << "OK" << std::endl;
        std::cout << document[ "a" ].GetString() << std::endl;
    }
}

This is the output when executed:

{ "a": { "z" : 21 } } 
OK
JSONTest: ../rapidjson/document.h:441: const typename Encoding::Ch* rapidjson::GenericValue<Encoding, Allocator>::GetString() const [with Encoding = rapidjson::UTF8<char>, Allocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]: Assertion `IsString()' failed. Aborted

How do I retrieve the inner object to continue my parsing? Thanks.

Edit: What I need is to obtain the string representation of the inner object so I can call another function that is going to parse it.

Edit 2: code that allows to retrieve the inner object as a string:

rapidjson::Document document;
std::string test =  "{\"a\":{\"z\":21}} ";
if ( document.Parse<0>( test.c_str() ).HasParseError() ) {
    std::cout << "Error parsing" << std::endl;
} else {
    if ( document[ "a" ].IsObject() ) {
        rapidjson::StringBuffer sb;
        rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
        document[ "a" ].Accept( writer );
        std::cout << sb.GetString() << std::endl;
    }
}
like image 790
pparescasellas Avatar asked Oct 05 '12 08:10

pparescasellas


People also ask

What is RapidJSON used for?

RapidJSON is a JSON parser and generator for C++. It was inspired by RapidXml. RapidJSON is small but complete. It supports both SAX and DOM style API.

Does JSON allow nested objects?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

How do I use RapidJSON in Visual Studio?

In Visual Studio, open the solution and in Solution Explorer, select the project that is using rapidjson and either use the main menu PROJECT Properties function, or right-click on the project and select Properties from the option menu. This will display the Properties dialog.


1 Answers

You need to iterate through object's members manually, as GetString() only works on string members, while document["a"] is an Object. You need to iterate through that object's members using MemberIterator variable. I had no practice in C* for more than 15 years, so I can only give a general idea of how it should work:

for (MemberIterator m = document["a"].MemberBegin(); m != document["a"].MemberEnd(); ++m) {
    std::cout << m.name << " " << (m.IsNumber()?m.GetNumber():m.GetString()) << endl;
}

Also, you might want to look at Accept() method, it seems to return a JSON string of an object you give it.

like image 62
Vesper Avatar answered Sep 21 '22 14:09

Vesper