title: Retrieving a nested object inside a JSON string using rapidjson
body: <p>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:</p>
  
  <pre><code>rapidjson::Document document;
  std::string test =  " { \"a\": { \"z\" : 21 } } ";
  std::cout &lt;&lt; test &lt;&lt; std::endl;
  if ( document.Parse&lt;0&gt;( test.c_str() ).HasParseError() ) {
      std::cout &lt;&lt; "Parsing error" &lt;&lt; std::endl;
  } else {
      if ( document[ "a" ].IsObject() ) {
          std::cout &lt;&lt; "OK" &lt;&lt; std::endl;
          std::cout &lt;&lt; document[ "a" ].GetString() &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>This is the output when executed:</p>
  
  <pre><code>{ "a": { "z" : 21 } } 
  OK
  JSONTest: ../rapidjson/document.h:441: const typename Encoding::Ch* rapidjson::GenericValue&lt;Encoding, Allocator&gt;::GetString() const [with Encoding = rapidjson::UTF8&lt;char&gt;, Allocator = rapidjson::MemoryPoolAllocator&lt;rapidjson::CrtAllocator&gt;]: Assertion `IsString()' failed. Aborted
  </code></pre>
  
  <p>How do I retrieve the inner object to continue my parsing? Thanks.</p>
  
  <p><strong>Edit</strong>: 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.</p>
  
  <p><strong>Edit 2</strong>: code that allows to retrieve the inner object as a string:</p>
  
  <pre><code>rapidjson::Document document;
  std::string test =  "{\"a\":{\"z\":21}} ";
  if ( document.Parse&lt;0&gt;( test.c_str() ).HasParseError() ) {
      std::cout &lt;&lt; "Error parsing" &lt;&lt; std::endl;
  } else {
      if ( document[ "a" ].IsObject() ) {
          rapidjson::StringBuffer sb;
          rapidjson::Writer&lt;rapidjson::StringBuffer&gt; writer( sb );
          document[ "a" ].Accept( writer );
          std::cout &lt;&lt; sb.GetString() &lt;&lt; std::endl;
      }
  }
  </code></pre>