I'm trying to convert a Json::Value into a std::string.
Unfortunately JsonCpp Library throws an std::runtime_error exception which says "Type is not convertible to string".
I'm trying to convert the following JSON document:
I'm not sure what you mean by convert a json value into an std::string, but if you get an exception, you are likely using Json::Value::asString(). This methods should only be used to return the string stored in a Json::Value (e.g.on the node "devices" in your example).
To serialize the Json::Value into a Json document, you should use a Json writer, or Json::Value::toStyledString().
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Of course, I forgot to mention that I used toString. Neither toCString works.
Well, I guess I'm just using json-cpp the wrong way. Can anyone tell me how to use the writer correctly? I already searched for documentations and just found the API description.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
if you're trying to convert all json to string this is an error. Only a "key" : "value" can be converted to string. If you simply want to output json you have you can you ToStyledString() which does what you expect.
To parse the above json you might use v.asString() and so on. Remember you can inspect what you have with functions like isArray() isObject() isString() and so on.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello folks,
I'm trying to convert a Json::Value into a std::string.
Unfortunately JsonCpp Library throws an std::runtime_error exception which says "Type is not convertible to string".
I'm trying to convert the following JSON document:
{
"devices" :
{
"0" : "asdf1",
"1" : "asdf2",
"2" : "asdf3"
},
"response" : "get_device_list"
}
Can anyone help me solve this problem?
I'm not sure what you mean by convert a json value into an std::string, but if you get an exception, you are likely using Json::Value::asString(). This methods should only be used to return the string stored in a Json::Value (e.g.on the node "devices" in your example).
To serialize the Json::Value into a Json document, you should use a Json writer, or Json::Value::toStyledString().
Thanks for your reply!
Of course, I forgot to mention that I used toString. Neither toCString works.
Well, I guess I'm just using json-cpp the wrong way. Can anyone tell me how to use the writer correctly? I already searched for documentations and just found the API description.
if you're trying to convert all json to string this is an error. Only a "key" : "value" can be converted to string. If you simply want to output json you have you can you ToStyledString() which does what you expect.
To parse the above json you might use v.asString() and so on. Remember you can inspect what you have with functions like isArray() isObject() isString() and so on.