https://github.com/josdejong/svelte-jsoneditor
https://jsoneditoronline.org/docs/index.html
JSON相关文档 https://jsoneditoronline.org/indepth
在线 JSON解析器 https://jsoneditoronline.org

可以展开,压缩,排序,精确报错,command+f搜索,格式化,前进回退;
基本能满足 JSON 查看、编辑、格式化、转换和验证需求;
重要的是:复制粘贴长一点的json,页面不卡顿。

  1. 查看和编辑 JSON
  2. 具有低级文本编辑器和高级树视图和表格视图
  3. 格式化(美化)和压缩 JSON
  4. 排序、查询、过滤和转换 JSON
  5. 修复 JSON,Repair JSON
  6. JSON 模式验证和可插入的自定义验证
  7. 颜色突出显示、撤消/重做、搜索和替换
  8. 颜色选择器和时间戳标签等实用程序
  9. JSON文件最大不要超过 512 MB

五种编辑查看模式

  1. code
  2. 树形
  3. 文本
  4. 表单
  5. 视图

属性

mainMenuBar={false} 不显示工具条
content支持 json对象,和 text字符串json,只能二选一

  1. const props = {
  2. content: {
  3. text: undefined, // 解析字符串的 json
  4. json: {}, // 解析的 json文档
  5. }
  6. }

readOnly 只读

image.png

text 代码模式

mode=’text’,默认 mode=tree
https://jsoneditoronline.org/docs/index.html#text-mode

JsonEditor

json编辑器的 React版本
image.png

  1. import React, { useEffect, useRef } from 'react';
  2. import {
  3. bool,
  4. object,
  5. oneOf,
  6. number,
  7. func,
  8. } from 'prop-types';
  9. import { JSONEditor } from 'vanilla-jsoneditor';
  10. import { noop, isFunction } from 'lodash-es';
  11. const defaultContent = {
  12. text: undefined,
  13. json: {},
  14. mode: 'text',
  15. }
  16. JsonEditor.propTypes = {
  17. content: object,
  18. mode: oneOf(['text', 'tree', 'table']),
  19. tabSize: number,
  20. indentation: number,
  21. readOnly: bool,
  22. mainMenuBar: bool,
  23. navigationBar: bool,
  24. onChange: func,
  25. };
  26. JsonEditor.defaultProps = {
  27. content: defaultContent,
  28. mode: 'text',
  29. tabSize: 2, // Tab 默认 4
  30. indentation={2} // 缩进默认 4
  31. readOnly: false,
  32. mainMenuBar: false,
  33. navigationBar: false,
  34. onChange: noop,
  35. }
  36. function JsonEditor(props) {
  37. const refContainer = useRef(null);
  38. const refEditor = useRef(null);
  39. useEffect(() => {
  40. // create editor
  41. refEditor.current = new JSONEditor({
  42. target: refContainer.current,
  43. props: {}
  44. });
  45. return () => {
  46. // destroy editor 销毁
  47. refEditor.current?.destroy();
  48. refEditor.current = null;
  49. };
  50. }, []);
  51. // update props
  52. useEffect(() => {
  53. refEditor.current?.updateProps(props);
  54. }, [props]);
  55. return (
  56. <div
  57. className="flex flex-1"
  58. ref={refContainer}
  59. />
  60. );
  61. }
  62. export default JsonEditor;

onRenderMenu 自定义菜单

  1. import JsonEditor from '../JsonEditor'
  2. function App() {
  3. const [content, setContent] = useState({ json: {} });
  4. function handleRenderMenu(items, context) {
  5. // console.log('handleRenderMenu', { items, context })
  6. // 分割线
  7. // const separator = { separator: true }
  8. // 自定义按钮
  9. // const customCopyButton = {
  10. // onClick: handleCopy,
  11. // icon: <CopyOutlined />,
  12. // title: 'Copy document to clipboard',
  13. // className: 'custom-copy-button'
  14. // }
  15. // 空格
  16. // const space = { space: true }
  17. const result = items.filter((it: any, i: number) => i > 3);
  18. return result;
  19. // const itemsWithoutSpace = items.slice(0, items.length - 2)
  20. // return itemsWithoutSpace.concat([separator, customCopyButton, space])
  21. }
  22. function handleChange(updatedContent, previousContent, { contentErrors, patchResult }) {
  23. console.log('onChange: ', { updatedContent, previousContent, contentErrors, patchResult })
  24. setContent(updatedContent)
  25. }
  26. return (
  27. <JsonEditor
  28. content={content}
  29. onChange={handleChange}
  30. onRenderMenu={handleRenderMenu}
  31. // parser 自定义 JSON解析器
  32. // validationParser 自定义 JSON验证器
  33. />
  34. )
  35. }

validationParser

自定义 JSON验证器
无损json解析 https://github.com/josdejong/lossless-json

树形

方便的查看模式,支持展开折叠,基本的编辑功能,甚至类型的确定,位置的转换,全局搜索精确定位

图标不显示问题
原因:调用的是一张大的雪碧图,来源是本地静态地址;css里通过background-position去确定其位置从而决定用哪个图标

jsoneditor常见问题

光标错位问题

设置字体为12px即可