C++ JSONCPP 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4289986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:03:08  来源:igfitidea点击:

JSONCPP Writing to files

c++jsonjsoncpp

提问by Yelnats

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, values, strings, et cetera and write them into the file?

JSONCPP 有一个 writer,但它似乎所做的只是从解析器获取信息,然后将其输出到字符串或流中。我如何让它改变或创建新的对象、数组、值、字符串等并将它们写入文件?

回答by cegprakash

#include<json/writer.h>

Code:

代码:

    Json::Value event;   
    Json::Value vec(Json::arrayValue);
    vec.append(Json::Value(1));
    vec.append(Json::Value(2));
    vec.append(Json::Value(3));

    event["competitors"]["home"]["name"] = "Liverpool";
    event["competitors"]["away"]["code"] = 89223;
    event["competitors"]["away"]["name"] = "Aston Villa";
    event["competitors"]["away"]["code"]=vec;

    std::cout << event << std::endl;

Output:

输出:

{
        "competitors" : 
        {
                "away" : 
                {
                        "code" : [ 1, 2, 3 ],
                        "name" : "Aston Villa"
                },
                "home" : 
                {
                        "name" : "Liverpool"
                }
        }
}

回答by kk-robot

#include <json.h>
#include <iostream>
#include <fstream>

void main()
{
    std::ofstream file_id;
    op_file_id.open("file.txt");

    Json::Value value_obj;
    //populate 'value_obj' with the objects, arrays etc.

    Json::StyledWriter styledWriter;
    file_id << styledWriter.write(value_obj);

    file_id.close();
}

回答by Marcelo Cantos

AFAICT, you create objects of type Json::Value, which caters for all the JSON data-types, and pass the result to a Json::Writer (one of its derived types, to be specific), or simply to a stream.

AFAICT,您创建类型为 Json::Value 的对象,该对象满足所有 JSON 数据类型,并将结果传递给 Json::Writer(其派生类型之一,具体来说),或简单地传递给流。

E.g.: to write an array of three integers to a file:

例如:将三个整数的数组写入文件:

Json::Value vec(Json::arrayValue);
vec.append(Json::Value(1));
vec.append(Json::Value(2));
vec.append(Json::Value(3));
std::cout << vec;

回答by Matthew Flaschen

First, you have to create the desired JSON::Value. You should look at all the constructors (first). To create the necessary hierarchies, see appendand the operator[] overloads; there are overloads for both array indicesand string keysfor objects.

首先,您必须创建所需的JSON::Value. 您应该查看所有构造函数(首先)。要创建必要的层次结构,请参阅append和 operator[] 重载;对象的数组索引字符串键都有重载。

One way to write the JSON value back out is using StyledStreamWriter::writeand ofstream.

将 JSON 值写回的一种方法是使用StyledStreamWriter::writeofstream

See cegprakash's answer for how to write it.

有关如何编写它,请参阅 cegprakash 的答案。

回答by Searene

Json::StyledWriteris deprecated, you can use Json::StreamWriterBuilderto write json into files.

Json::StyledWriter已弃用,您可以使用Json::StreamWriterBuilder将 json 写入文件。

Json::Value rootJsonValue;
rootJsonValue["foo"] = "bar";

Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = "   ";

std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::ofstream outputFileStream("/tmp/test.json");
writer -> write(rootJsonValue, &outputFileStream);

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

json 将被写入/tmp/test.json.

$ cat /tmp/test.json

{
    "foo" : "bar"
}