如何在Ballerina中打印格式化的JSON值

cpjpxq1n  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(96)

请运行以下示例代码。

import ballerina/io;

public function main() {
    json j = {"id": 1, "name": "John", "address": {"city": "New York", "country": "USA"}};
    io:println(j);
}

字符串
这里j在控制台中打印,没有格式化,如下所示。

{"id":1,"name":"John","address":{"city":"New York","country":"USA"}}


打印格式化的JSON值的推荐方法是什么?

jxct1oxe

jxct1oxe1#

您可以使用thisarug/prettify包来漂亮地打印JSON值。

import ballerina/io;
import thisarug/prettify;

public function main() {
    json j = {"id": 1, "name": "John", "address": {"city": "New York", "country": "USA"}};
    io:println(prettify:prettify(j));
}

字符串
这段代码将打印以下内容:

{
    "id": 1,
    "name": "John",
    "address": {
        "city": "New York",
        "country": "USA"
    }
}

相关问题