簡體   English   中英

使JSON易於閱讀(R)

[英]Make json easily readable (R)

假設我有一個變量ouput ,其中包含以下形式的json:

{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}

我應該怎么做才能轉換ouput ,使其易於閱讀? 我想要這樣的東西:

hello1 : bla1
hello2 : bla2
hello3 :
         hello31 : hello311 : 7078
                   hello312 : 3429
         hello32 : hello321 : 10
                   hello322 : 6
         hello33 : hello331 : 4.6317
                   hello332 : 2.6322

如果只想檢查json ,則可以使用package listviewer ,它在RStudio中呈現了一個不錯的小部件。

# install.packages("listviewer")

output <- '{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}'

listviewer::jsonedit(output)

使用jsonliteprettify函數,您可以獲得與輸出類似的東西。

# Your object
my_json_object <- '{"hello1":["bla1"],"hello2":["bla2"],"hello3":{"hello31":{"hello311":[7078],"hello312":[3429]},"hello32":{"hello321":[10],"hello322":[6]},"hello33":{"hello331":[4.6317],"hello332":[2.6322]}}}'


prettify(my_json_object, indent = 2)
{
"hello1": [
    "bla1"
],
"hello2": [
    "bla2"
],
"hello3": {
    "hello31": {
        "hello311": [
            7078
        ],
        "hello312": [
            3429
        ]
    },
    "hello32": {
        "hello321": [
            10
        ],
        "hello322": [
            6
        ]
    },
    "hello33": {
        "hello331": [
            4.6317
        ],
        "hello332": [
            2.6322
        ]
    }
}
}

暫無
暫無

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

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