Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择JSON未格式化代码,并将格式化的JSON漂亮地打印到DIV中_Javascript_Json_Format_Pretty Print - Fatal编程技术网

Javascript 选择JSON未格式化代码,并将格式化的JSON漂亮地打印到DIV中

Javascript 选择JSON未格式化代码,并将格式化的JSON漂亮地打印到DIV中,javascript,json,format,pretty-print,Javascript,Json,Format,Pretty Print,我有一个函数,它接收一个选择文本,这是一个未格式化的JSON代码,我试图使用stringify方法将JSON代码格式化为一个漂亮的视图。代码如下: function prettyPrintJSON(selectionInfo) { const unformattedJSON = selectionInfo.selectionText const formattedJSON = JSON.stringify(unformattedJSON, null, '\t') } 这是未格式化的J

我有一个函数,它接收一个选择文本,这是一个未格式化的JSON代码,我试图使用stringify方法将JSON代码格式化为一个漂亮的视图。代码如下:

 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"}}]} 
这就是它在表中的外观:

正如你所看到的,它没有格式化。另外,我在google chrome扩展上使用它,并使用vue.js作为框架构建一个表,在同一个表中输出不同类型的数据。你知道我错过了什么吗?谢谢

将该\t转换为一个数字,表示您需要多少缩进空格

让json={colors:black,category:hue,type:primary,code:{rgba:[255255255,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,0255,1],hex:00F},hue,primary:hue,code:{rgba:[0,0255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]颜色[255255,0,1],十六进制:FF0},{颜色:绿色,类别:色调,类型:次要,代码:{rgba:[0255,0,1],十六进制:0F0}]} ;
console.logJSON.stringifyjson,null,2;//间距级别=2;从JSON.stringifyunformatedjson,null,“\t”中删除null和\t,使其仅为JSON.stringifyunformatedjson我刚刚尝试了这一点,输出看起来仍然一样。我发现,我缺少了显示的标记。我正打算告诉你这一点。