JSON文字列内のネストされたオブジェクトを取得する必要があり、rapidjsonを使用して取得しようとしています。私が見つけたのは、配列と基本型を取得する方法だけですが、サブオブジェクトは取得しません。エラーが発生する次のおもちゃの例を作成しました。
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;
}
}
実行時の出力は次のとおりです。
{ "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
解析を続行するために内部オブジェクトを取得するにはどうすればよいですか?ありがとう。
編集:必要なのは、内部オブジェクトの文字列表現を取得して、それを解析しようとしている別の関数を呼び出すことができるようにすることです。
編集2:内部オブジェクトを文字列として取得できるようにするコード:
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;
}
}