C++ POCO - 如何美化一个JSON?

标签 c++ json poco-libraries

我使用 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++ POCO - 如何美化一个JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52218674/

相关文章:

c++ - QModelIndex 作为 parent ?

ios - 无法使用 Wordpress 中的高级自定义字段值

c++ - Poco::Net::NoMessageException 在 HTTP/GET 后几秒引发

C# 无法使用数组反序列化复杂的 Json 对象

c++ - DirectoryIterator 可能存在的错误

c++ - POCO - 我可以使用 SocketReactor 作为客户端吗?

c++ - 有人对 gcc 的 LTO (C++) 有经验吗?

c++ - cin.getline() 不适用于 switch 语句

C++ 损坏的程序 - WPRFLAG?

asp.net - 将 JSON 对象反序列化为 List<type> 不适用于 asmx 服务