C++ 将 Json::Value 转换为 std::string?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29710306/
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 21:13:49  来源:igfitidea点击:

Converting a Json::Value to std::string?

c++stdstringjsoncpp

提问by BRabbit27

I am using JsonCppto build a JSON object. Once the object is built, is there a way I can get the object as an std::string?

我正在使用JsonCpp构建一个 JSON 对象。构建对象后,有没有办法将对象作为std::string?

回答by Jordan Doyle

You can use a Json::Writerto do exactly this, since I assume you want to save it somewhere so you don't want human readable output, your best bet would be to use a Json::FastWriterand then you can call the writemethod with the parameter of your Json::Value(ie. your root) and then that simply returns a std::stringlike so:

您可以使用Json::Writer来做到这一点,因为我假设您想将它保存在某个地方,所以您不希望人类可读的输出,您最好的选择是使用Json::FastWriter,然后您可以调用write带有你的Json::Value参数的方法(即你的根),然后简单地返回一个std::string像这样的:

Json::FastWriter fastWriter;
std::string output = fastWriter.write(root);

回答by mts knn

Json::Writeris deprecated and Json::StreamWriterBuildershould instead be used, as stated in the documentation of Json::Writer.

Json::Writer已被废弃,Json::StreamWriterBuilder应改为使用,如在规定的文件Json::Writer

Json::writeStringwrites into a stringstream and then returns a string:

Json::writeString写入一个字符串流,然后返回一个字符串:

Json::Value json = ...;
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // If you want whitespace-less output
const std::string output = Json::writeString(builder, json);

Kudos to cdunn2001's answer here: How to get JsonCPP values as strings?

感谢 cdunn2001 的回答:How to get JsonCPP values as strings?

回答by Searene

If your Json::valueis of string type, e.g. "bar" in the following json

如果您Json::value是字符串类型,例如以下json中的“bar”

{
    "foo": "bar"
}

You can use Json::Value.asStringto get the value of barwithout extra quotes(which will be added if you use StringWriterBuilder). Here is an example:

您可以使用Json::Value.asString来获取bar没有额外引号的值(如果使用 将添加StringWriterBuilder)。下面是一个例子:

Json::Value rootJsonValue;
rootJsonValue["foo"] = "bar";
std::string s = rootJsonValue["foo"].asString();
std::cout << s << std::endl; // bar

回答by Mathieu Chateauvert

In my context, I instead used a simple .asString()at the end of my json value object. As @Searene said, it gets rid of the extra quotes that you don't necessary wants if you want to process it after.

在我的上下文中,我在 json 值对象的末尾使用了一个简单的.asString()。正如@Searene 所说,如果您想在之后处理它,它会删除您不需要的额外引号。

Json::Value credentials;
Json::Reader reader;

// Catch the error if wanted for the reader if wanted. 
reader.parse(request.body(), credentials);

std::string usager, password;
usager = credentials["usager"].asString();
password = credentials["password"].asString();

If the value is a int instead of a string, .asInt()works great as well.

如果值是 int 而不是字符串,.asInt()也能很好地工作。

回答by Kokeb

You can also use the method toStyledString().

您还可以使用 toStyledString() 方法。

jsonValue.toStyledString();

The method "toStyledString()" converts any value to a formatted string. See also the link: doc for toStyledString

方法“ toStyledString()”将任何值转换为格式化字符串。另请参阅链接:toStyledString 的文档