-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Problem while reading a json file #2161
Comments
You try to assign a string to a number. You need to make that conversion explicit, e.g. with std::to_string. |
For extracting every single parameter from a json file i was using the below code: |
You need to do this: x = std::to_string(element); But this only works if if (element.is_string())
{
x = std::to_string(element);
} |
As per your suggestion, i have modified the code as below:
But getting the below error: Error C2668 'std::to_string': ambiguous call to overloaded function I have also tried stringstream to convert the string into a number. But it does not work either. |
Oh, x = std::stoi(element.get_ref<std::string&>()); |
The above-mentioned line throws the below error: |
Then change it to x = std::stoi(element.get_ref<const std::string&>()); |
This worked for me. Thanx a lot for your kind help. |
First of all thanks for this nice library.
I have the json in below format:
{
"values": "false",
"tran_up_left_scene_x": "1",
"CRC": "145"
}
I have saved it as input.json file and want to read it using the below code:
int x;
int y;
std::ifstream ifs("C:/codes/input.json");
json j = json::parse(ifs);
for (const auto& element : j["tran_up_left_scene_x"])
{
std::cout << element << std::endl;
x = element;
}
But I am getting the below error:
Exception unhandled:
default:
JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
The json validator is showing the above used json is valid.
Any kind of help to solve this problem will be highly appreciated.
The text was updated successfully, but these errors were encountered: