-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to retrieve data from nested objects #1038
Comments
You could try for (const auto& item : json1["items"]["item"])
{
// check if item["batters"]["batter"] exists
if (item.find("batters") != item.end() and
item["batters"].find("batter") != item["batters"].end())
{
for (const auto& batter : item["batters"]["batter"])
{
std::cout << batter["id"] << ", " << batter["type"] << std::endl;
}
}
// check if item["topping"] exists
if (item.find("topping") != item.end())
{
for (const auto& batter : item["topping"])
{
std::cout << batter["id"] << ", " << batter["type"] << std::endl;
}
}
} |
This does answer my initial question, but now I get a parsing error.
For an unknown reason it doesn't accept the parsing. |
What is the error? |
It just stops, no error or anything. It tried printing after each statement and it happens at the JSON parsing. |
Can you check whether you can print the file content? Can you run the code in a debugger? |
After a few hours trying to debug, I finally got it to compile properly (reinstalled MinGW, changed my paths, etc.). The above code that @nlohmann wrote is accurate and works fine! |
Thanks for checking back! |
How can one push batter["id"] into another json object or vector. I'm getting the following error for above query: |
Hey, thanks for your example code.
while this returns 1
my debugger is unable to unpack the type so im just wondering, why is the first 0 and the second 1? |
Can you share the complete code example and JSON file? |
thanks for you fast response. {
"title": "Data Transform Configuration",
"type": "generic",
"properties": {
"operations":[
{
"unique_id": "left_shift_1",
"type": "operation_shift",
"direction": "left",
"bit_start": 0,
"bit_length": 32,
"bit_shift": 4
}
],
"rules": {
"input": {},
"output": [
{
"lengthNumberType": "byte",
"dataLength":12,
"descritpion": "format either via references or direct in place, example shows in-place",
"format": [
{
"node_type": "text",
"unique_id": "1",
"content": "urn:epc:tag:grai-96:"
},
{
"node_type": "data",
"unique_id": "2",
"byte": 1,
"bit_start": 0,
"bit_length": 3,
"format": "decimal_ascii"
}
]
}
],
"notify": {
}
}
}
} the example code did not contain that much more. std::ifstream i(configFilePath);
i >> jFile;
std::cout << jFile.dump(4) << std::endl; where the following went with ok for (const auto &output : jFile["properties"]["rules"]["output"])
{
if (!(output.contains(Constants::KEY_LENGTH_NUMBER_TYPE)) ||
!(output.contains(Constants::KEY_RULE_DATA_LENGTH)) ||
!(output.contains(Constants::KEY_FORMAT)))
{
std::cout << "error while parsing. incomplete rule definition" << std::endl;
return;
}
...
} but when i used
instead of the for-loop it just gave 0 for the contains method |
If you iterate it in the |
Thank you very much for the clarification! Thank you for the fast response |
Hi, I'm trying to map data from a Json file to individual objects in order to sort them and perform other operations on them. However, the data structure required is quite complex and makes it hard to navigate.
Here is a snippet of the Json file:
I cannot change the way it is organised. However, I cannot figure out how to properly get every element and extract it.
My current code:
Note: Even then I get an error because
json2.get<json::object_t>()
returns a pair<>.Thanks in advance!
The text was updated successfully, but these errors were encountered: