Search code examples
jsoncpp

How to read this string into jsoncpp 's Json::Value


I have such a json string :

{"status":0,"bridge_id":"bridge.1","b_party":"85267191234","ref_id":"20180104151432001_0","function":{"operator_profile":{"operator":"aaa.bbb"},"subscriber_profile":{"is_allowed":true,"type":8},"name":"ServiceAuthen.Ack"},"node_id":"aaa.bbb.collector.1"}

how can I read it into jsoncpp lib 's Json::Value object ?

I found such code by searching stackoverflow :

    std::string strJson = "{\"mykey\" : \"myvalue\"}"; // need escape the quotes

Json::Value root;   
Json::Reader reader;
bool parsingSuccessful = reader.parse( strJson.c_str(), root );     //parse process
if ( !parsingSuccessful )
{
    std::cout  << "Failed to parse"
           << reader.getFormattedErrorMessages();
    return 0;
}
std::cout << root.get("mykey", "A Default Value if not exists" ).asString() << std::endl;
return 0;

but how to convert my string to this form ?

{\"mykey\" : \"myvalue\"}

thank you for any help .


Solution

  • You don't.

    The slash characters are escape characters used to represent a " in C++ source code (without them the " would mean "This is the end of this C++ string literal").

    The JSON (which isn't C++ source code) should not have the escape characters in it.