Skip to content
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

Closed
sqwunkly opened this issue Sep 11, 2017 · 10 comments
Closed

nesting json objects #737

sqwunkly opened this issue Sep 11, 2017 · 10 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@sqwunkly
Copy link

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:-
image

What is most direct way to achieve this?

Thanks.

@leozzyzheng
Copy link

outerObj[ "duration" ] = nestedObj;
result is outerObj itself.

BTW, you should ask it in Stack Overflow not here.

@nlohmann
Copy link
Owner

An object stores key/value pairs. In your example nestedObj is not a string, so this can't work.

Try this:

json j_duration = { {"hours", 17}, {"mins, 18}, {"secs", 19} };
json j_misting = { {"isMisting", true}, {"duration", j_duration} };
json result = { {"misting, j_misting } };

@nlohmann
Copy link
Owner

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Sep 11, 2017
@leozzyzheng
Copy link

@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:)

@nlohmann
Copy link
Owner

I know, but some people are quite active here, so I don't bother having questions here.

@leozzyzheng
Copy link

Yeah, I see them :)

@nlohmann
Copy link
Owner

@sqwunkly Does this work for you?

@sqwunkly
Copy link
Author

@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!

image

@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.

@nlohmann
Copy link
Owner

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;
}
  • The [] operator works with strings, so there is no need to call c_str().
  • it.value() gives you the value for the element with key it.key().

@sqwunkly
Copy link
Author

that is much better.. thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants