-
-
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
nesting json objects #737
Comments
BTW, you should ask it in Stack Overflow not here. |
An object stores key/value pairs. In your example Try this: json j_duration = { {"hours", 17}, {"mins, 18}, {"secs", 19} };
json j_misting = { {"isMisting", true}, {"duration", j_duration} };
json result = { {"misting, j_misting } }; |
@leozzyzheng No, this is the right place to ask, see https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22kind%3A%20question%22%20. |
@nlohmann Oh, sorry, I usually think report bugs or ask features could open an issue. Ask how to use should read document first. Just my view:) |
I know, but some people are quite active here, so I don't bother having questions here. |
Yeah, I see them :) |
@sqwunkly Does this work for you? |
@nlohmann @leozzyzheng Yes, I think so.. it's still early stages but I think I can achieve all the building of nested json objects using your square brackets notation. This code below works well for converting and storing my json data into an array of corresponding nlohmann_json objects. It's not a great example of deep nesting but as far as I can tell any level of nesting can be achieved in this manner. So, unless I am mistaken, as long as I have the "name" (as variable char array, or as string literal) I'll be able to use this notation:- j_object[name_1][name_2][name_3} = j_object_to_be_nested for building nested objects to any desired depth. Now that I understand it this method is neat and succinct and I am happy with it. Niels, as I get further I'll let you know how I go. Much thanks! @leozzyzheng It may have seemed to you like I didn't read the docs for my question was basic, however I had spent a lot of time (a few days) reading the documents and googling. I've not done much C++ programming for 10 years and I've done zero modern c++ . Even after doing my utmost to understand the docs much still wasn't clear to me and I needed some experienced help. I do appreciate your's and Niel's. Cheers. |
You can simplify your for loop as follows: for (auto it = j_program->begin(); it != j_program->end(); ++it)
{
j_sessionsArray[i][it.key()] = it.value();
++i;
++sessionsCount;
}
|
that is much better.. thank you! |
RE: Nesting json objects
Is there a way to directly nest json objects in similar manner?
json outerObj = { "misting" , { "isMisting" , true } };
json nestedObj = { "duration" , { "hours" , 2}, { "mins" , 30 }, {"secs" , 20 } };
json result = outerObj[ nestedObj ]; // does not work
To obtain this:-
What is most direct way to achieve this?
Thanks.
The text was updated successfully, but these errors were encountered: