下面是从贴图编辑器导出的JSON文件。
{ "compressionlevel":-1,
"height":32,
"infinite":false,
"layers":[
{
"data":[ A whole bunch of integers in here],
"height":32,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":32,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.7.2",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"..\/..\/..\/..\/Desktop\/tileset001.tsx"
}],
"tilewidth":32,
"type":"map",
"version":"1.6",
"width":32
}
在这个C++块中,我试图解析出我实际需要的数据。
std::ifstream inFStream(filePath, std::ios::in);
if(!inFStream.is_open())
{
printf("Failed to open map file: &s", filePath);
}
rapidjson::IStreamWrapper inFStreamWrapper{inFStream};
rapidjson::Document doc{};
doc.ParseStream(inFStreamWrapper);
_WIDTH = doc["width"].GetInt(); //get width of map in tiles
_HEIGHT = doc["height"].GetInt(); //get height of map in tiles
const rapidjson::Value& data = doc["layers"]["data"]; //FAILURE POINT
assert(data.IsArray());
当我编译时,我能够为"layers" :[{}]
之外的宽度和高度提取正确的值,但是当那个const rapidjson::Value& data = doc["layers"]["data"];
被调用时,我会得到一个运行时错误,声称document.h第1344行IsObject()
断言失败。
我上上下下地浏览了rapidjson网站和其他资源,找不到像这样的东西。
下一步是获取存储在“数据”中的int值,并将它们推入std::vector
,但在我找到如何访问“数据”之前,这是不可能发生的。
发布于 2022-01-23 00:39:01
doc['layers']
是一个数组。
const rapidjson::Value& layers = doc["layers"];
assert(layers.IsArray());
for (size_t i=0; i < layers.Size(); i++) {
const rapidjson::Value& data = doc["layers"][i]["data"];
assert(data.IsArray());
}
更新:
直接访问layers
中的第一个layers
项
const rapidjson::Value& data = doc["layers"][0]["data"];
这只为data
数组中的第一个项提供了layers
。如果layers
至少有一项,而您只需要第一项,那么这将始终有效。
https://stackoverflow.com/questions/70818266
复制相似问题