簡體   English   中英

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM