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

Problem while reading a json file #2161

Closed
MdAnayat opened this issue Jun 4, 2020 · 8 comments
Closed

Problem while reading a json file #2161

MdAnayat opened this issue Jun 4, 2020 · 8 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@MdAnayat
Copy link

MdAnayat commented Jun 4, 2020

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.

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

You try to assign a string to a number. You need to make that conversion explicit, e.g. with std::to_string.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 4, 2020
@MdAnayat
Copy link
Author

MdAnayat commented Jun 4, 2020

For extracting every single parameter from a json file i was using the below code:
for (const auto& element : j["tran_up_left_scene_x"])
{
std::cout << element << std::endl;
x = element;
}
But using std::to_string how can i extract each parameter of the json file??
can you give a code example??

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

You need to do this:

x = std::to_string(element);

But this only works if element holds a string. You can check the type with:

if (element.is_string())
{
    x = std::to_string(element);
}

@MdAnayat
Copy link
Author

MdAnayat commented Jun 4, 2020

As per your suggestion, i have modified the code as below:
int x;
int y;
std::ifstream ifs("C:/codes/input.json");
json j = json::parse(ifs);
for (const auto& element : j["CRC"])
{

	std::cout << element << std::endl;
	if (element.is_string())
	{
		x = std::to_string(element);
	}
}

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.

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

Oh, std::to_string makes no sense here.

x = std::stoi(element.get_ref<std::string&>());

@MdAnayat
Copy link
Author

MdAnayat commented Jun 4, 2020

The above-mentioned line throws the below error:
Error C2662 'ReferenceType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>::get_refstd::string&,0(void)': cannot convert 'this' pointer from 'const nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>>' to 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer,std::vector<uint8_t,std::allocator<_Ty>>> &'

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

Then change it to

x = std::stoi(element.get_ref<const std::string&>());

@MdAnayat
Copy link
Author

MdAnayat commented Jun 4, 2020

This worked for me. Thanx a lot for your kind help.
You can close this issue now.

@nlohmann nlohmann closed this as completed Jun 4, 2020
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