JSON 문자열에서 중첩 된 객체를 가져와야하고 rapidjson을 사용하여이를 수행하려고합니다. 내가 찾은 것은 배열과 기본 유형을 검색하는 방법이지만 하위 객체는 검색하지 않는 것입니다. 내 구문 분석을 계속 내부 개체를 검색하려면 어떻게rapidjson을 사용하여 JSON 문자열 내에서 중첩 된 객체 검색
{ "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
:
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;
}
}
이 실행 출력이 : 나는 오류가 제공하는 다음과 같은 장난감 예를 만들었습니다? 감사.
편집 : 필요한 것은 내부 개체의 문자열 표현을 가져 와서 구문 분석 할 다른 함수를 호출 할 수 있습니다.
편집 2 : 문자열로 내부 객체를 검색 할 수 있습니다 코드 :
당신하여 GetString()가 전용 문서하면서, 선재에서 작동하기 때문에, 수동으로 개체의 멤버를 반복 할 필요가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;
}
}
RapidJson이 계층 적 객체를 지원하지 않는다는 의미입니까? 그래서 그것은 오직 루트 레벨을 구문 분석합니까?!?! –