json可视化树

1、概要

近期项目需要集成第三方webservice服务,前端界面需要将第三方请求json,返回json的可视化展示。

2、效果

借用第三方插件展示 vue-json-viewer
在这里插入图片描述
利用Reflect递归查找赋值
在这里插入图片描述

3、数据

请求数据

{"type":"object","properties":{"header":{"type":"object","properties":{"AuthHeader":{"type":"object","properties":{"User":{"description":"User","type":"string"},"Pwd":{"description":"Pwd","type":"string"}}}}},"body":{"type":"object","properties":{"student[Student]":{"description":"student[Student]","type":"array","items":{"type":"object","properties":{"MyProperty1":{"description":"MyProperty1","type":"string"},"MyProperty":{"description":"MyProperty","type":"string"}}}}}}}}

响应数据

{"type":"object","properties":{"header":{"type":"object","properties":{}},"body":{"type":"object","properties":{"HelloWorldResult[Student]":{"description":"HelloWorldResult[Student]","type":"array","items":{"type":"object","properties":{"MyProperty1":{"description":"MyProperty1","type":"string"},"MyProperty":{"description":"MyProperty","type":"string"}}}}}}}}

4、代码

借用第三方插件展示 vue-json-viewer

<template>
  <s-modal subtitle="Json格式化" :operationType="operationType" :maskClosable="false" :destroyOnClose="true" :visible="visible" width="900px" centered
    @ok="handleOk" @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <div>
        <h3>请求json</h3>
      <json-viewer :value="requestJsonData" :expand-depth=5 copyable boxed sort></json-viewer>
        <h3>响应json</h3>
      <json-viewer :value="responseJsonData" :expand-depth=5 copyable boxed sort></json-viewer>
      </div>
    </a-spin>

  </s-modal>
</template>

<script>
import Vue from 'vue'
import { SModal, AreaTitle } from '@/components'
import JsonViewer from 'vue-json-viewer'
Vue.use(JsonViewer)

export default {
  name: 'WebserviceJsonShow',
  components: {
    SModal,
    AreaTitle,
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
      operationType: undefined,
      requestJsonData: {
        data: [],
      },   responseJsonData: {
        data: [],
      },
    }
  },
  methods: {
    view(record) {
      console.log('record: ', record)
      this.visible = true
      this.operationType = this.operation.view
      this.requestJsonData.data.push(JSON.parse(record.requestJson))
          this.responseJsonData.data.push(JSON.parse(record.responseJson))
    },
    handleOk() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    },
  },
}
</script>

利用Reflect递归查找赋值

    //json序列化
    initInputDataFromJsonSchema(jsonSchema) {
      const dataInputs = []
      var root = this.jsonSchemaToData(jsonSchema)
      dataInputs.push(root)
      return dataInputs
    },
    jsonSchemaToData(jsonSchema) {
      //console.info(jsonSchema)
      let jsonObj = JSON.parse(jsonSchema)
      //console.info(jsonObj)
      let nodeType = this.reflectStringValue(jsonObj, 'type')
      //var title = this.reflectStringValue(jsonObj, 'title')
      let root = {
        key: 'root',
        name: 'root',
        dataType: '',
        isArray: false,
        children: new Array(),
        //mock: this.reflectStringValue(jsonObj, 'mock'),
        description: this.reflectStringValue(jsonObj, 'description'),
      }
      this.setDataFromJsonSchema(jsonObj, root)
      //console.info(root)
      return root
    },
    setDataFromJsonSchema(jsonObj, parent) {
      if (!jsonObj) return
      let nodeType = this.reflectStringValue(jsonObj, 'type')
      if (nodeType == 'object' && Reflect.has(jsonObj, 'properties')) {
        parent.dataType = nodeType
        parent.children = new Array()
        let nodeProperties = Reflect.get(jsonObj, 'properties')
        if (!nodeProperties) return
        Reflect.ownKeys(nodeProperties).forEach((item) => {
          let itemValue = Reflect.get(nodeProperties, item)
          let child = this.initChildItem(itemValue, item)
          this.setDataFromJsonSchema(itemValue, child)
          parent.children.push(child)
        })
      } else if (nodeType == 'array' && Reflect.has(jsonObj, 'items')) {
        parent.dataType = nodeType
        parent.isArray = true
        parent.children = new Array()
        let nodeProperties = Reflect.get(jsonObj, 'items')
        let child = this.initChildItem(jsonObj, 'items')
        this.setDataFromJsonSchema(nodeProperties, child)
        parent.children.push(child)
      } else if (nodeType == 'number') {
        parent.dataType = nodeType
      } else {
        parent.dataType = nodeType
      }
    },
    initChildItem(obj, name) {
      let child = {
        key: this.itemKey++,
        name: name,
        isArray: false,
        dataType: this.reflectStringValue(obj, 'type'),
        //mock: this.reflectStringValue(obj, 'mock'),
        description: this.reflectStringValue(obj, 'description'),
      }
      return child
    },

    reflectStringValue(obj, property) {
      let value = Reflect.get(obj, property)
      if (value) return value
      else return ''
    },

5、总结

  1. 两种方式可视化json数据
  2. vue-json-viewer和reflect的使用
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值