【问题标题】:How to read this string into jsoncpp 's Json::Value如何将此字符串读入 jsoncpp 的 Json::Value
【发布时间】:2018-01-04 09:30:15
【问题描述】:

我有这样一个json字符串:

{"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"}

如何将其读入 jsoncpp lib 的 Json::Value 对象?

我通过搜索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;

但是如何将我的字符串转换为这种形式?

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

感谢您的帮助。

【问题讨论】:

    标签: jsoncpp


    【解决方案1】:

    你没有。

    斜杠字符是用于表示 C++ 源代码 中的" 的转义字符(没有它们," 将意味着“这是此 C++ 字符串文字的结尾”)。

    JSON(不是 C++ 源代码)中不应包含转义字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 2012-02-09
      • 2015-01-02
      • 1970-01-01
      相关资源
      最近更新 更多