簡體   English   中英

如何使用 rapidjson 從 object 內部解析 JSON 數組

[英]How to parse JSON array from inside an object with rapidjson

以下是從 Tiled Map 編輯器導出的 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中,但在我弄清楚如何訪問“數據”之前,這不會發生

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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM