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

Read a subkey from json object #1740

Closed
leochou0729 opened this issue Sep 6, 2019 · 2 comments
Closed

Read a subkey from json object #1740

leochou0729 opened this issue Sep 6, 2019 · 2 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@leochou0729
Copy link

Hello,
I try to use the following code to get a boolean subkey.
bool switch = !config["Function"]["Enable"].is_null() ? config["Function"]["Enable"] : true;

But the build process failed with the "Expected unqualified-id" message.
I'm using Xcode with c++14 support.

BTW, what's the correct way to read arbitrary key value(no matter in what level) to a variable? Thanks!

@nlohmann
Copy link
Owner

nlohmann commented Sep 8, 2019

There are two issues:

  • switch is a C++ keyword. You cannot use this as a name for a variable.
  • To deduct the type of the variable, all branches need to return the same type. true is bool, but config["Function"]["Enable"] is json. The latter can be converted as follows:
!config["Function"]["Enable"].is_null() ? config["Function"]["Enable"].get<bool>() : true;

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 8, 2019
@leochou0729
Copy link
Author

Thanks a lot!

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

2 participants