[英]Select JSON unformatted code and pretty print formatted JSON into DIV
我有一個 function 接受選擇文本,該文本是未格式化的 JSON 代碼,我試圖使用字符串化方法將 JSON 代碼格式化為(pretty 視圖)。 這是代碼:
function prettyPrintJSON(selectionInfo) {
const unformattedJSON = selectionInfo.selectionText
const formattedJSON = JSON.stringify(unformattedJSON, null, '\t')
}
這是未格式化的 JSON 代碼:
{"colors":[{"color":"black","category":"hue","type":"primary","code":{"rgba":[255,255,255,1],"hex":"#000"}},{"color":"white","category":"value","code":{"rgba":[0,0,0,1],"hex":"#FFF"}},{"color":"red","category":"hue","type":"primary","code":{"rgba":[255,0,0,1],"hex":"#FF0"}},{"color":"blue","category":"hue","type":"primary","code":{"rgba":[0,0,255,1],"hex":"#00F"}},{"color":"yellow","category":"hue","type":"primary","code":{"rgba":[255,255,0,1],"hex":"#FF0"}},{"color":"green","category":"hue","type":"secondary","code":{"rgba":[0,255,0,1],"hex":"#0F0"}}]}
如您所見,它沒有格式化。 此外,我在谷歌瀏覽器擴展上使用它並使用 vue.js 作為框架來構建一個表到 output 同一個表中不同類型的數據。 有什么我想念的想法嗎? 謝謝
把那個\t
變成一個數字來表示你想要多少個縮進空格
let json = {"colors":[{"color":"black","category":"hue","type":"primary","code":{"rgba":[255,255,255,1],"hex":"#000"}},{"color":"white","category":"value","code":{"rgba":[0,0,0,1],"hex":"#FFF"}},{"color":"red","category":"hue","type":"primary","code":{"rgba":[255,0,0,1],"hex":"#FF0"}},{"color":"blue","category":"hue","type":"primary","code":{"rgba":[0,0,255,1],"hex":"#00F"}},{"color":"yellow","category":"hue","type":"primary","code":{"rgba":[255,255,0,1],"hex":"#FF0"}},{"color":"green","category":"hue","type":"secondary","code":{"rgba":[0,255,0,1],"hex":"#0F0"}}]}; console.log(JSON.stringify(json, null, 2)); // spacing level = 2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.