2017-03-09 1 views
0

오늘 중첩 된 값으로 문서를 구문 분석 할 수 있는지 확인하기 위해 RapidJSON 라이브러리를 테스트했는데 어떤 이유로 오류에 대한 해결책을 찾을 수 없었습니다. 지고 있었다. Google 또는 Stack Overflow를 한두 시간 동안 검색하여 해결 방법을 찾지 못했습니다. 다음 오류와 함께 코드는 다음과 같습니다어설 션`IsArray() '실패 (RapidJSON)

MAIN.CPP :

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include "rapidjson/document.h" 

#include "include.hpp" 

int main() { 
    unsigned int input = 1; 
    tile output; 
    output = LoadTile("../locations.json", input); 

    std::cout << output.x << std::endl; 

    return 0; 
} 

load.cpp

#include <iostream> 
#include "rapidjson/document.h" 
#include "rapidjson/filereadstream.h" 

#include "include.hpp" 

using namespace rapidjson; 

tile LoadTile(std::string fileName, unsigned int number) { 
    FILE* file = fopen(fileName.c_str(), "r"); 

    char buffer[2048]; 
    FileReadStream stream(file, buffer, 2048); 

    Document doc; 
    doc.ParseStream(stream); 

    tile output; 
    Value& tileNumber = doc[number]; 

    if(!tileNumber.IsObject()) { 
     output.overflow = true; 
     output.x = 0; 
     output.y = 0; 
     output.type = "\0"; 
    }else{ 
     output.x = tileNumber[0]["x"].GetInt(); 
     output.y = tileNumber[0]["y"].GetInt(); 
     output.type = tileNumber[0]["type"].GetString(); 
    } 

    return output; 
} 

include.hpp :

#include <iostream> 
#include <SFML/Graphics.hpp> 
#include "rapidjson/document.h" 

struct tile { 
int x; 
int y; 
std::string type; 
bool overflow = false; 
}; 

tile LoadTile(std::string fileName, unsigned int number); 

CMakeLists.txt :

cmake_minimum_required(VERSION 2.6) 
project(test) 

set(EXECUTABLE_NAME "test") 
add_executable(${EXECUTABLE_NAME} main.cpp load.cpp include.hpp) 

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH}) 

install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin}) 
,210

locations.json :

{ 
    1:[ 
     {"x":32}, 
     {"y":32}, 
     {"type":"water_c"} 
    ] 
} 

오류 :

test: /home/.../rapidjson/document.h:1547:rapidjson::GenericValue<Encoding, Allocator>::operator[](rapidjson::SizeType) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::SizeType = unsigned int]: Assertion `IsArray()' failed. 
Aborted (core dumped) 

나는 모든 것을 시도했습니다, 그것은 JSON 형식이 아니다 알고있다. 뭔가가 없다면 은 정말입니다. 나는 이것을 Xubuntu 16.10에서 실행하고있다. 도움을 줄 수있는 사람 덕분입니다.

답변

0

JSON이 유효하지 않습니다. JSON에서 키는 큰 따옴표로 묶은 문자열이어야합니다. 더 자세한 내용 here. JSONLint을 사용하여 JSON 문자열의 유효성을 검사하는 것이 좋습니다. 올바른 JSON은 다음과 같습니다 (큰 따옴표 안에 1).

{ 
    "1": [{ 
     "x": 32 
    }, { 
     "y": 32 
    }, { 
     "type": "water_c" 
    }] 
}