JSONCPP Writing to files

后端 未结 5 1942
我在风中等你
我在风中等你 2020-12-24 02:26

JSONCPP has a writer, but all it seems to do is get info from the parser and then output it into a string or a stream. How do I make it alter or create new objects, arrays,

5条回答
  •  鱼传尺愫
    2020-12-24 03:08

    Json::StyledWriter is deprecated, you can use Json::StreamWriterBuilder to write json into files.

    Json::Value rootJsonValue;
    rootJsonValue["foo"] = "bar";
    
    Json::StreamWriterBuilder builder;
    builder["commentStyle"] = "None";
    builder["indentation"] = "   ";
    
    std::unique_ptr writer(builder.newStreamWriter());
    std::ofstream outputFileStream("/tmp/test.json");
    writer -> write(rootJsonValue, &outputFileStream);
    

    The json will be written into /tmp/test.json.

    $ cat /tmp/test.json
    
    {
        "foo" : "bar"
    }
    

提交回复
热议问题