C++ POCO - 如何美化一个JSON?
C++ POCO - How to beautify a JSON?
我使用 POCO
库生成一个 JSON
文件,如下所示:
void writeToFile()
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object;
json->set("name", "foo");
json->set("address", "bar");
std::ostringstream oss;
Poco::JSON::Stringifier::stringify(json, oss);
std::ofstream ofs("output.json");
if (ofs.is_open() == true)
{
ofs << oss.str();
ofs.close();
}
}
output.json
包含:
{"name":"foo","address":"bar"}
POCO
有没有办法美化一个JSON
?
所以输出会是这样的:
{
"name" : "foo",
"address" : "bar"
}
正如@Dmitry 在评论中所说,stringify()
方法中的参数可以:
static void stringify(
const Dynamic::Var & any,
std::ostream & out,
unsigned int indent = 0,
int step = - 1,
int options = Poco::JSON_WRAP_STRINGS
);
示例:
Poco::JSON::Stringifier::stringify(json, oss, 4, -1, Poco::JSON_PRESERVE_KEY_ORDER);
我使用 POCO
库生成一个 JSON
文件,如下所示:
void writeToFile()
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object;
json->set("name", "foo");
json->set("address", "bar");
std::ostringstream oss;
Poco::JSON::Stringifier::stringify(json, oss);
std::ofstream ofs("output.json");
if (ofs.is_open() == true)
{
ofs << oss.str();
ofs.close();
}
}
output.json
包含:
{"name":"foo","address":"bar"}
POCO
有没有办法美化一个JSON
?
所以输出会是这样的:
{
"name" : "foo",
"address" : "bar"
}
正如@Dmitry 在评论中所说,stringify()
方法中的参数可以:
static void stringify(
const Dynamic::Var & any,
std::ostream & out,
unsigned int indent = 0,
int step = - 1,
int options = Poco::JSON_WRAP_STRINGS
);
示例:
Poco::JSON::Stringifier::stringify(json, oss, 4, -1, Poco::JSON_PRESERVE_KEY_ORDER);