asString() Exceptions

  • immnn

    immnn - 2010-05-18

    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?

  • Baptiste Lepilleur

    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().

  • immnn

    immnn - 2010-05-22

    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.

  • Dmitrii Sernii

    Dmitrii Sernii - 2012-07-30

    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.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks