So I have a JSON string I get using cURL that I'm trying to parse for data using JSON for Modern C++ (nlohmann::json). Here is my code:
double retValue(string data) {
string str;
double value = 0;
try {
auto jsonData = json::parse(data.c_str());
str = jsonData["layer"][1]["Page"]["Number"];
value = stoi(str);
}
catch(json::parse_error& e) {
cout << "Error: " << e.what() << endl;
return 0;
}
return value;
}
So In PHP json_decode works fine to decode into an array and the values can be easily parsed in this way, but I am having trouble with C++ and this library. I get the following error at run time but compiles fine:
terminate called after throwing an instance of 'nlohmann::detail::type_error' what(): [json.exception.type_error.305] cannot use operator[] with object Aborted (core dumped)
The JSON data is similar to this I'm trying to parse and I figure being multidimensional is the problem and that I'm not handling the data properly.
{
"layer": {
"1": {
"Page": {
"Number": 3.14
}
}
}
}
Can anybody point me in the right direction?