react-json-view示例
// import the react-json-view component
import ReactJson from 'react-json-view'
// use the component in your app!
<ReactJson src={my_json_object} />
在Hooks中使用react-json-view
import React, { useState } from 'react';
import ReactJson from 'react-json-view';
interface IProps { }
const Index: React.FC<IProps> = (props) => {
const reactJsonView = {
"string": 'this is test ...',
"integer": 42,
'array': [1, 'two', NaN],
'float': 3.14159,
'undefined': undefined,
'object': {},
'string_number': '1234'
}
const [reactJson, setReactJson] = useState<object>(reactJsonView);
const propsVal = {
name: false, // JSON數(shù)據(jù)的根節(jié)點(diǎn)(用默認(rèn)或指定的根節(jié)點(diǎn)包裹自己的數(shù)據(jù))
style: { wordBreak: 'break-all' }, // 換行
src: { reactJson }, // 需要展示的JSON數(shù)據(jù)
theme: 'rjv-default', // 支持base-16主題
iconStyle: 'circle', // circle(圓)、triangle(三角形)、square(正方形)
indentWidth: 6, // 首行縮進(jìn)長度
collapsed: 2, // 節(jié)點(diǎn)折疊
collapseStringsAfterLength: false, // 超出內(nèi)容會(huì)變成…的功能
displayDataTypes: true, // 數(shù)據(jù)類型會(huì)出現(xiàn)在數(shù)據(jù)的前綴值
displayObjectSize: true, // 對(duì)象和數(shù)組被標(biāo)記為大
}
return <ReactJson {...propsVal} />
}
export default Index;
Demo展示

Demo展示
Props屬性
| Name | Type | Default | Description |
|---|---|---|---|
| src | JSON Object | None | 需要展示的JSON數(shù)據(jù) |
| name | string or false | "root" | JSON數(shù)據(jù)的根節(jié)點(diǎn)(用默認(rèn)或指定的根節(jié)點(diǎn)包裹自己的數(shù)據(jù)),使用null或false沒有名字 |
| theme | string | "rjv-default" | RJV支持base-16主題 |
| style | object | {} | 可以通過style添加、修改樣式,可覆蓋主題默認(rèn)提供的屬性 |
| iconStyle | string | "circle" | 接受參數(shù):circle(圓)、triangle(三角形)、square(正方形) |
| indentWidth | integer | 4 | JSON嵌套對(duì)象的縮進(jìn)值 |
| collapsed | boolean or integer | false / number | 默認(rèn)情況下展開全部節(jié)點(diǎn),當(dāng)設(shè)置為true時(shí),所有節(jié)點(diǎn)都將被折疊。使用整數(shù)值在特定深度折疊 |
| collapseStringsAfterLength | integer | false | 這個(gè)就是超出內(nèi)容會(huì)變成…的功能。當(dāng)一個(gè)整數(shù)值被賦值時(shí),字符串就會(huì)在這個(gè)長度后面接上省略號(hào)??梢酝ㄟ^單擊字符串值來展開和折疊字符串內(nèi)容 |
| shouldCollapse | (field)=>{} | false | 回調(diào)函數(shù)來提供對(duì)默認(rèn)情況下應(yīng)該折疊的對(duì)象和數(shù)組的控制。對(duì)象被傳遞給包含name, src, type(“數(shù)組”或“對(duì)象”)和namespace |
| groupArraysAfterLength | integer | 100 | 當(dāng)指定整數(shù)值時(shí),數(shù)組將按值的計(jì)數(shù)分組顯示。組以括號(hào)表示,可以通過單擊括號(hào)展開和折疊 |
| enableClipboard | boolean or (copy)=>{} | true | 當(dāng)prop不為false時(shí),用戶可以通過單擊剪貼板圖標(biāo)將對(duì)象和陣列復(fù)制到剪貼板。支持復(fù)制回調(diào) |
| displayObjectSize | boolean | true | 當(dāng)設(shè)置為true,對(duì)象和數(shù)組被標(biāo)記為大小。例如: { a: ‘a(chǎn)1’,b: ‘b1’ },會(huì)顯示2 items |
| displayDataTypes | boolean | true | 當(dāng)設(shè)置為true,數(shù)據(jù)類型會(huì)出現(xiàn)在數(shù)據(jù)的前綴值.例如: { a: 123, b: ‘b1’},會(huì)顯示{ a: int 123, b: string ‘b1’} |
| onEdit | (edit)=>{} | false | 當(dāng)傳入回調(diào)函數(shù)時(shí),edit功能已啟用。在編輯完成之后調(diào)用回調(diào) |
| onAdd | (add)=>{} | false | 當(dāng)傳入回調(diào)函數(shù)時(shí),add功能已啟用。在完成添加之后調(diào)用回調(diào) |
| defaultValue | string / number / boolean / array / object | null | 設(shè)置將項(xiàng)添加到j(luò)son時(shí)使用的默認(rèn)值 |
| onDelete | (delete)=>{} | false | 當(dāng)傳入回調(diào)函數(shù)時(shí),delete功能已啟用。在完成刪除之后調(diào)用回調(diào) |
| onSelect | (select)=>{} | false | 當(dāng)傳入函數(shù)時(shí),單擊值將觸發(fā)onSelect方法將被調(diào)用 |
| sortKeys | boolean | false | 設(shè)置為true以對(duì)對(duì)象鍵進(jìn)行排序 |
| quotesOnKeys | boolean | true | 設(shè)置為false以刪除鍵中的引號(hào)(例如“name”:vs.name:) |
| validationMessage | string | "Validation Error" | OneEdit、onAdd或onDelete回調(diào)驗(yàn)證失敗的自定義消息 |
| displayArrayKey | boolean | true | 當(dāng)設(shè)置為true時(shí),元素的索引前綴值 |