V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
beryl
V2EX  ›  程序员

c++11 json 字符串转义问题-正则?

  •  1
     
  •   beryl · 2018-03-29 15:51:10 +08:00 · 2536 次点击
    这是一个创建于 2221 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 json 字符串变量为

    {
        "a": 1,
        "b": 2
    }
    

    想转义其为 json 字符串

    {\"a\": 1, \"b\": 2}
    

    是个变量所以没有办法使用 R"()" 这种方式,json 库使用的为jsoncpp 请问有什么好的方法么?

    2 条回复    2018-03-29 16:49:53 +08:00
    prolic
        1
    prolic  
       2018-03-29 16:43:49 +08:00
    string json_str = json_encode(old_json);
    new_json["str"] = Json::Value(json_str);
    正好用过这个库,大概这样处理
    顺便这不是一个 c++11json 问题,是个 jsoncpp 问题
    beryl
        2
    beryl  
    OP
       2018-03-29 16:49:53 +08:00
    @prolic 嗯嗯,看了一下 jsoncpp 的文档,用:
    ```
    Json::FastWriter fastWriter;
    std::string out = fastWriter.write(json_value);
    ```
    现在使用这种方式解决了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2694 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:08 · PVG 19:08 · LAX 04:08 · JFK 07:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.