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

[answered]Read key1 from .value example #914

Closed
Diablillowilly opened this issue Jan 14, 2018 · 5 comments
Closed

[answered]Read key1 from .value example #914

Diablillowilly opened this issue Jan 14, 2018 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Diablillowilly
Copy link

I was trying to use the library to read and parse a json post which I receive, so I just want to read each value, but , I dont know how to read a value inside an object. In this example I would like to access the value of key1 or key2, I have tryed things like object.key1 or object:key1, but none of them worked.

@nlohmann
Copy link
Owner

Assume you read this JSON value:

{
  "key1": true,
  "key2": 2
}

Then you can access the values with:

bool k1 = j["key1"];
int k2 = j["key2"];

(assuming you parsed your JSON value to json j).

If you need more information, please provide some example code, the error you experience and your expected behavior.

@Diablillowilly
Copy link
Author

Diablillowilly commented Jan 14, 2018

I get a json in a string from a http post

`std::string input=
"{

    {\"str\", \"helloworld\"},
    {\"int\", 24},
    {\"object\", {{\"key1\", 1}, {\"key2\", 2}, {\"subobject\",{\"subkey1\", 1}, {\"subkey2\", 2}}}},
}";`

and after that I parse them like this so I get rid of the backslash:
nlohmann::json j = nlohmann::json::parse(input);

and I am reading values at the same level as str or int by making:

std::string str = j.value("str", "");
 int str = j.value("int", 0);

but what do I have to do If I want to read key1/2 or subkey1/2?

EDIT

And is there any difference between int k2 = j["key2"]; or int k2 = j.value("key2", 0);?
Whaat would happen if I used the first method and k2 didnt exist?

@nlohmann
Copy link
Owner

  • Right now, value does not support nesting. That is, you can only apply it to an object to retrieve a primitive value.
  • j["key"] adds key "key" if it does not exist and stores null. If j is const, the behavior is undefined for nonexisting keys.
  • j.at("key") throws an exception if key "key" does not exist and will not change the object.

@cronnosli
Copy link

"but what do I have to do If I want to read key1/2 or subkey1/2?"

If you want to read a subkey you could access the json object as it is like a multilevel array:

int value = j["object"]["subobject"][subkey1].get<int>();

Or by walk throw in a STL manner:

for (json::iterator it = j["object"].begin(); it != j["object"].end(); ++it) {
  std::cout << it.key() << " : " << it.value() << "\n";
}

But with a loop you would need to interact each subobject in a nested loop!

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jan 16, 2018
@Diablillowilly
Copy link
Author

I have finally made it like this:

if (!j["object"]["subobject"]["subkey"].is_null()) { return j["object"]["subobject"]["subkey"]; }

So I check if it exists, and then I read it. Thank you very much for your help

@Diablillowilly Diablillowilly changed the title Read key1 from .value example [answered]Read key1 from .value example Jan 19, 2018
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