Converting json value to int in C ++

I am reading json value in C ++ using

Json::Reader reader 

and the value is stored in Json::Value root

This root contains "age" and "id", and I want to convert root ["age"] to int.

I tried converting it to a string using .str () but couldn't get it.

Any suggestion?

+6
source share
2 answers

In jsoncpp they provide helper methods for the Json::Value object. You can simply call the asInt() method on the value to convert it.

 int ageAsInt = root["age"].asInt() 
+8
source

You should be able to use

 std::stoi( string ) 

Example taken from http://en.cppreference.com/w/cpp/string/basic_string/stol

 #include <iostream> #include <string> int main() { std::string test = "45"; int myint = std::stoi(test); std::cout << myint << '\n'; } 
0
source

Source: https://habr.com/ru/post/957125/


All Articles