-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Retrieving array elements. #1369
Comments
How does the JSON value in |
What do you mean? The json file? https://i.imgur.com/v4kdPeF.png |
Can you paste it here? I cannot parse images :-) |
{
"Cube":{
"information":{
"name":"",
"authors":""},
"slotType":"main",
//--Nodes--
"nodes":[
["id", "posX", "posY", "posZ"],
["nl0",0.26,-0.26,-0.26],
["nl1",0.26,-0.26,0.26],
["nr2",-0.26,-0.26,-0.26],
["nr3",-0.26,-0.26,0.26],
["nl4",0.26,0.26,-0.26],
],
//--Beams--
"beams":[
["id1:", "id2:"],
["nl1","nl0"],
["nl0","nl4"],
["nl5","nl4"],
["nr7","nl5"],
],
//--tri col--
"triangles":[
["id1:", "id2:", "id3:"],
["nr3","nr6","nr2"],
["nr7","nl4","nr6"],
["nr7","nr3","nl1"],
],
}
} |
@kepler425b the Json String posted by you was a bit incorrect, at least according to https://jsonlint.com/
Note the hierarchy involved in the Json here As such what your code should consist of doing is first performing a find on the Cube and verify if it exists. Note that just like Regular Find implemented by STL and also by RapidJson, Nlohmann's Json returns end() when find fails To give you an idea, check out the code here
A small mistake you performed was that you decided to search directly for nodes within the main Json rather than follow the hierarchy of cube -> nodes Now coming to your loop, I am not sure if it's correct or incorrect...
This obviously has the benefit of looking simpler and being simpler to use. Notice that your if statement has a minor issue as well. If you are still confused, feel free to ask |
@kepler425b Did @pratikpc 's answer help you? |
data is a json document, "nodes" is an array containing arrays in the document.
The confusion I have with this library is when I get returned an object type iterator and when array type iterator. For example using a "find" function as in this line:
json::iterator c = data.find("nodes");
I expect to retrieve direct iterator for that array, but I get returned an object type.How would you iterate the "nodes" array and access elements of it?
This loop throws exception but I'll just paste it here to get an idea of what I am trying to achieve:
While in rapidjson I do it this way:
and it's quite intuitive what it does, you get and iterator and it holds a value to whatever it has, you use that pointer to retrieve elements. If it's array type, you just use [] operator to index into elements.
I read the docs of nlohmann library many times, looked through examples but I didn't find them useful.
The text was updated successfully, but these errors were encountered: