I've been trying to parse a JSON string that looks like:
[1, 2, 3, 4]
Here's my code:
#include <iostream>
#include "rapidxml/document.h"
void main()
{
char const *json_str = "[1, 2, 3, 4]";
rapidjson::Document d;
d.Parse(json_str);
if(json_str.IsArray())
{
for(auto &e: d)
{
std::cout << e.GetInt() << std::endl;
}
}
}
Unfortunately, the code fails to compile with "begin() is not declared in this scope" and other errors of the same sort.
This indicates that I'm trying to iterate over something that can't have std::begin() and std::end() called on it.
The rapidjson tutorial here only explains how to iterate over an array retrieved using GetArray(), and not at the document level. I also went through the docs, and searched on Google without much luck. I also tried using:
for(auto &e: document.GetArray()) // Similar to the tutorial's code which calls d.GetObject()
But this fails saying that GenericDocument (Document is a typedef, AFAIK) does not have a member called GetArray(). For that matter, it doesn't have a GetObject() member either.
On the other hand, iterating using a counter works just fine.
I might be missing something obvious since it's been a long day, and it's 12:00 AM in my part of the world, but I'd really appreciate any pointers on where I might be going wrong, and how to complete this task.
Aucun commentaire:
Enregistrer un commentaire