首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >断言“`IsArray()”失败(RapidJSON)

断言“`IsArray()”失败(RapidJSON)
EN

Stack Overflow用户
提问于 2017-03-09 03:47:03
回答 1查看 3K关注 0票数 0

我今天早些时候对RapidJSON库进行了测试,以确定是否可以解析带有嵌套值的文档,而且由于某种原因,我无法找到解决所获得的错误的解决方案。我在Google和Stack溢出搜索了一两个小时,却找不到修复方法。下面是代码和错误:

main.cpp:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
    1:[
        {"x":32},
        {"y":32},
        {"type":"water_c"}
    ]
}

错误:

代码语言:javascript
复制
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上运行这个。感谢任何能帮忙的人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-09 10:20:15

您的JSON无效。在JSON中,键必须是字符串,用双引号编写。更多细节这里。我建议使用JSONLint来验证JSON字符串。有效的JSON看起来如下(1在双引号中):

代码语言:javascript
复制
{
    "1": [{
        "x": 32
    }, {
        "y": 32
    }, {
        "type": "water_c"
    }]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42686284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档