• 请不要在回答技术问题时复制粘贴 AI 生成的内容
beryl
V2EX  ›  程序员

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

  •  1
     
  •   beryl · Mar 29, 2018 · 2989 views
    This topic created in 3002 days ago, the information mentioned may be changed or developed.

    一个 json 字符串变量为

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

    想转义其为 json 字符串

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

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

    2 replies    2018-03-29 16:49:53 +08:00
    prolic
        1
    prolic  
       Mar 29, 2018
    string json_str = json_encode(old_json);
    new_json["str"] = Json::Value(json_str);
    正好用过这个库,大概这样处理
    顺便这不是一个 c++11json 问题,是个 jsoncpp 问题
    beryl
        2
    beryl  
    OP
       Mar 29, 2018
    @prolic 嗯嗯,看了一下 jsoncpp 的文档,用:
    ```
    Json::FastWriter fastWriter;
    std::string out = fastWriter.write(json_value);
    ```
    现在使用这种方式解决了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3173 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:03 · PVG 20:03 · LAX 05:03 · JFK 08:03
    ♥ Do have faith in what you're doing.