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
Converting a Json::Value to std::string?
提问by BRabbit27
回答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 write
method with the parameter of your Json::Value(ie. your root) and then that simply returns a std::string
like so:
您可以使用Json::Writer来做到这一点,因为我假设您想将它保存在某个地方,所以您不希望人类可读的输出,您最好的选择是使用Json::FastWriter,然后您可以调用write
带有你的Json::Value参数的方法(即你的根),然后简单地返回一个std::string
像这样的:
Json::FastWriter fastWriter;
std::string output = fastWriter.write(root);
回答by mts knn
Json::Writer
is deprecated and Json::StreamWriterBuilder
should instead be used, as stated in the documentation of Json::Writer
.
Json::Writer
已被废弃,Json::StreamWriterBuilder
应改为使用,如在规定的文件Json::Writer
。
Json::writeString
writes 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::value
is of string type, e.g. "bar" in the following json
如果您Json::value
是字符串类型,例如以下json中的“bar”
{
"foo": "bar"
}
You can use Json::Value.asString
to get the value of bar
without 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 的文档