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

reading int number and string number #1986

Closed
Behrouz-m opened this issue Mar 14, 2020 · 5 comments
Closed

reading int number and string number #1986

Behrouz-m opened this issue Mar 14, 2020 · 5 comments
Labels
kind: question state: needs more info the author of the issue needs to provide more details

Comments

@Behrouz-m
Copy link

Behrouz-m commented Mar 14, 2020

Hi. I receive a JSON from 2 different places that contain a number. one of them returns JSON with a number and the other returns the same JSON with a number with quotes. like this:

{"state": 0}

or:

{"state": "0"}

the problem is how can I read them with no problem?

@nlohmann
Copy link
Owner

What is your exact issue? Deserialization (also know as parsing) is described here: https://github.com/nlohmann/json#serialization--deserialization

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 14, 2020
@Behrouz-m
Copy link
Author

my problem is that state can be number and also string that contains number. I want to assign this number to int variable but it is not possible to read it using normal assignment.

json jp = R"({"state": "10"})"_json;
int state = jp["state"]; // return 0 instead of value 10

json jp = R"({"state": 10})"_json;
int state = jp["state"]; // return 10 becuase it is number not string

@nlohmann
Copy link
Owner

You can call jp["state"].is_number() or jp["state"].is_string() to check the type.

@Behrouz-m
Copy link
Author

I know that. I am looking for an easier way instead of checking types for every number type in json. what about jp.get<int>("state") ?

@nlohmann
Copy link
Owner

The library does not provide such conversions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

2 participants