-1

I am trying to parse a Json file with rapidJson (v 0.11) but I fail to do it with what seems to be a simple array. Here is the structure of my file :

[{"id": "my_id1","type":"my_type1"},
 {"id": "my_id2","type":"my_type2"},
 ...
]

It seems that rapidJson does not read that type of file starting with an array.. Is there a way to handle that ?

Here is a extract of the code I tried to use :

FILE * pFile = fopen ("my_json.json" , "r"); rapidjson::FileStream is(pFile); rapidjson::Document document; document.ParseStream<0>(is); and i am stuck right here because i am supposed to access to the elements by this syntax value = document["key"], but I don't have any "key" at the first level...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arcyno
  • 4,153
  • 3
  • 34
  • 52

1 Answers1

2

Given that you have an array at the top level of your JSON file, you would simply access your document by providing an index into that array.

For example:

// access first element
rapidjson::Value & val = document[0];
zubron
  • 36
  • 1
  • Thx ! It might seems obvious but it didn't feel that way when i was searching... For those who gets an error message on `document[0]`, you can use `document[0u]` to solve it. – Arcyno Apr 09 '15 at 12:07
  • In newer versions at [GitHub](https://github.com/miloyip/rapidjson), no need to use `0u`. Please try the latest version. – Milo Yip Apr 15 '15 at 03:24