2.1.7 • Published 24 days ago

json-beautiful-render v2.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
24 days ago

中文 | English

npm

支持两个功能:

  • 将json数据美化成html,在原生javascript或react、vue等框架中使用。

image

  • 将json数据格式化,加入换行符、缩进符等,方便阅读。

image

安装

NPM

npm add json-beautiful-render

UMD

<script src="//cdn.jsdelivr.net/npm/json-beautiful-render/lib/umd/index.js"></script>

umd模式暴露了全局变量jsonRenderjsonRenderNoStyle

使用

  • 基础使用
import jsonRender from 'json-beautiful-render';

const dom = jsonRender(document.querySelector('#container'), {
    name: 'hello world',
});
  • 定制渲染的颜色
import jsonRender from 'json-beautiful-render';

jsonRender(
    document.querySelector('#container'),
    {
        name: 'hello world',
    },
    {
        valueColor: 'green',
    },
);
  • 将json数据格式化,加入换行符、缩进符等,方便阅读。
import { jsonRenderNoStyle } from 'json-beautiful-render';

const result = jsonRenderNoStyle(document.querySelector('#your_textarea'), { name: 'hello world' });
console.log(result);

API

jsonRender

(el, json, options) => HTMLElement

美化渲染,如果el是HTMLElement,则将渲染结果挂载到el上。

参数说明类型默认值
el挂载的容器HTMLElement | null | undefined-
jsonJSON数据Object-
options配置Options-

Options

配置项

参数说明类型默认值
activeBgColor项目激活时,整行区域的背景色string-
activeHighLightColor项目激活时,文字区域的背景色string-
levelHighLightColor项目激活时,该项目所有父级键名的颜色string-
labelColor键名的文本颜色string-
valueColor键值的文本颜色string-
valueColors不同变量类型的文本颜色ValueColors-
expand允许展开和收缩功能booleantrue
showItemsLength显示子元素的长度,collapse表示收缩时显示,always表示始终显示,其他值则不显示'collapse' | 'always' | false'collapse'

ValueColors

不同变量类型的文本颜色

参数说明类型默认值
string字符串string-
number数值string-
specialness特殊值,如boolean、nullstring-

jsonRenderNoStyle

(el, json, options) => string

无样式的格式化渲染,如果el是HTMLElement,则将渲染结果挂载到el容器上。

参数说明类型默认值
el挂载的容器HTMLElement | null | undefined-
jsonJSON数据Object-
options配置Options-

Options

配置项

参数说明类型默认值
indent缩进字符string-

FAQ

渲染哪些类型的值?

支持渲染标准的json值如"123"123,其余则渲染其类型如DateFunction等。

值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。详情见>>

2.1.7

24 days ago

2.1.6

3 months ago

2.1.5

3 months ago

2.1.2

4 months ago

2.1.3

4 months ago

2.1.1

4 months ago

2.1.0

4 months ago

2.0.0

4 months ago

1.2.0

4 months ago

1.2.1

4 months ago

1.1.4

4 months ago

1.1.3

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.1.2

4 months ago

1.0.1

5 months ago

1.0.0

5 months ago