2017-03-09 23 views
0

今日私はRapidJSONライブラリをテストして、ネストされた値でドキュメントを解析できるかどうかを確認しました。何らかの理由でエラーIやっていた。 GoogleやStack Overflowを1〜2時間かけて検索したところ、修正が見つかりませんでした。ここでエラーと一緒にコードがある:アサーション `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}) 

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" 
    }] 
} 
関連する問題