[英]jsoncpp formatting problems
I'm using jsoncpp and I'm having a problem with how the json messages are formatted when they are written using one of the Writers. 我正在使用jsoncpp,我在使用其中一个Writers编写json消息时遇到了问题。
For example: 例如:
root["name"] = "monkey";
std::cout << writer.write(root) << "\n";
Gives me something formatted like this 给了我这样格式的东西
{
"name" : "monkey"
}
While I actually want: 虽然我真的想要:
{"name":"monkey"}
I've looked at the documentation and there are mentions of setIndentLength()
but they don't appear in the source files, so maybe they are deprecated or something. 我查看了文档,并提到了setIndentLength()
但它们没有出现在源文件中,所以可能它们已被弃用或者其他东西。
Anyway anyone knows how to do this? 无论如何谁都知道如何做到这一点?
As an extension of cdunn2001's answer, there is no need to re-write default settings (.settings_). 作为cdunn2001答案的扩展,无需重写默认设置(.settings_)。 You can just override 'indentation' value of StreamWriterBuilder builder: 您可以覆盖StreamWriterBuilder构建器的“缩进”值:
Json::Value json = ...
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = ""; //The JSON document is written in a single line
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
If you use Jsoncpp version 1.1, you can use Json::FastWriter
instead of Json::StyledWriter
or Json::Writer
: 如果您使用Jsoncpp 1.1版,您可以使用Json::FastWriter
而不是Json::StyledWriter
或Json::Writer
:
The JSON document is written in a single line. JSON文档只用一行编写。 It is not intended for 'human' consumption, but may be usefull to support feature such as RPC where bandwith is limited. 它不是用于“人类”消费,但可能有助于支持诸如带宽有限的RPC之类的功能。
FastWriter
, StyledWriter
, StyledStreamWriter
, and Writer
are deprecated . 不推荐使用 FastWriter
, StyledWriter
, StyledStreamWriter
和Writer
。 Use StreamWriterBuilder
, which creates a StreamWriter
with a slightly different API. 使用StreamWriterBuilder
,它创建一个具有稍微不同的API的StreamWriter
。 Use it this way: 用这种方式:
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(root, &std::cout);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.