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);
相关文章:
- 在c++中使用nlohmann从类到json的转换
- 函数何时会在c++中包含stack_Unwind_Resume调用
- C++json插入数组
- nlohmann-json将一个数组插入到另一个数组中
- 如何使用curlpp通过POST方法上传文件和json数据
- Python中的for循环与C++有何不同
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 nlohmann 的 json 库中获取数组长度?
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray
- 如何在特定位置追加 json 文件
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 使用 jsoncpp 解析 json 数组字符串
- 将 FlatBuffer 对象序列化为 JSON,而不使用其架构文件
- 在C++中释放内存期间,迭代器与指针有何不同
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 使用 cmake 生成 compile_commands.json
- 如何使用 c++ 和提升库生成 json
- C++ POCO - 如何美化 JSON?