Search code examples
c++jsonc++11type-conversionjsoncpp

jsoncpp: convert string to double


I am trying to take a string, that I know represents a decimal, from a JSON object and assign it to a double in C++.

One would expect that asDouble() does the job, but this is not the case. For example if we have the array ["0.4983", "4387"] sitting in a variable Json::Value arr, doing

double x = arr[0].asDouble()

throws an exception Value is not convertible to double.

What is the recommended way of doing this (in C++ 11)?


Solution

  • My guess is that "0.4983" is a string, so jsoncpp refuses to convert it into a double. This is reasonable since normally to convert a string such as "abc" into a double makes no sense.

    What you need is to manually convert the string to double; in C++11 it would be stod.