目录
Json格式数据展示
vue解析json数据

Json格式数据展示
vue的jsonViewer组件也很好用,在网上看到有大神自己写的组件(递归调用),觉得很好,稍作修改,记录一下
JSON.stringify(obj, null, 4)
可以美化json数据的显示

<span class="light">要高亮的内容</span> 在json数据中,如果有需要高亮的内容,用以上标签包起来(这个要处理数据实现了,组件里高亮样式可以根据需要自己修改)
<template> <div class="bgView"> <div :class="['json-view', length ? 'closeable' : '']" :style="'font-size:' + fontSize+'px'"> <span @click="toggleClose" :class="['angle', innerclosed ? 'closed' : '']" v-if="length"></span> <div class="content-wrap"> <p class="first-line"> <span v-if="jsonKey" class="json-key">"{{jsonKey}}": </span> <span v-if="length" @click="toggleClose" style="cursor: pointer;"> {{prefix}} {{innerclosed ? ('... ' + subfix) : ''}} <!-- <span class="json-note"> {{innerclosed ? (' // count: ' + length) : ''}} </span> --> </span> <span v-if="!length">{{isArray ? '[]' : '{}'}}</span> </p> <div v-if="!innerclosed && length" class="json-body"> <template v-for="(item, index) in items"> <json-view v-if="item.isJSON" :closed="closed" :key="index" :json="item.value" :jsonKey="item.key" :depth="depth+1" :expandDepth="expandDepth" :isLast="index === items.length - 1"></json-view> <p v-else class="json-item" :key="index"> <span class="json-key"> {{(isArray ? '' : '"' + item.key + '":')}} </span> <span class="json-value" v-html="item.value + (index === items.length - 1 ? '' : ',')"/> </p> </template> <!-- <span v-show="!innerclosed" class="body-line"></span>--> </div> <p v-if="!innerclosed && length" class="last-line"> <span>{{subfix}}</span> </p> </div> </div> </div></template>
<script> export default { name: 'jsonView', props: { json: [Object, Array], jsonKey: { type: String, default: '' }, closed: { type: Boolean, default: true }, isLast: { type: Boolean, default: true }, fontSize: { type: Number, default: 13 }, expandDepth: { type: Number, default: 0 }, depth: { type: Number, default: 0 } }, created() { this.innerclosed = !this.closed ? this.closed : this.depth >= this.expandDepth this.$watch('closed', () => { this.innerclosed = this.closed }) }, data() { return { innerclosed: true } }, methods: { isObjectOrArray(source) { const type = Object.prototype.toString.call(source) const res = type === '[object Array]' || type === '[object Object]' return res }, toggleClose() { if (this.innerclosed) { this.innerclosed = false } else { this.innerclosed = true } } }, computed: { isArray() { return Object.prototype.toString.call(this.json) === '[object Array]' }, length() { return this.isArray ? this.json.length : Object.keys(this.json).length }, subfix() { return (this.isArray ? ']' : '}') + (this.isLast ? '' : ',') }, prefix() { return this.isArray ? '[' : '{' }, items() { if (this.isArray) { return this.json.map(item => { const isJSON = this.isObjectOrArray(item) return { value: isJSON ? item : JSON.stringify(item), isJSON, key: '' } }) } const json = this.json return Object.keys(json).map(key => { const item = json[key] const isJSON = this.isObjectOrArray(item) return { value: isJSON ? item : JSON.stringify(item), isJSON, key } }) } } }</script>
`<style>
.bgView {
background-color: #efefef;
font-family: NotoSansCJKkr;
}

  1. .json-view {
  2. position: relative;
  3. display: block;
  4. width: 100%;
  5. height: 100%;
  6. /* white-space: nowrap; */
  7. padding: 0 20px;
  8. box-sizing: border-box;
  9. line-height: 30px;
  10. }
  11. .json-note {
  12. color: #909399;
  13. }
  14. .json-key {
  15. color: #333333;
  16. }
  17. .json-value {
  18. color: #333333;
  19. }
  20. .json-item {
  21. margin: 0;
  22. padding-left: 20px;
  23. }
  24. .first-line {
  25. padding: 0;
  26. margin: 0;
  27. }
  28. .json-body {
  29. position: relative;
  30. padding: 0;
  31. margin: 0;
  32. }
  33. .json-body .body-line {
  34. position: absolute;
  35. height: 100%;
  36. width: 0;
  37. border-left: dashed 1px #bbb;
  38. top: 0;
  39. left: 2px;
  40. }
  41. .last-line {
  42. padding: 0;
  43. margin: 0;
  44. }
  45. .angle {
  46. position: absolute;
  47. display: block;
  48. cursor: pointer;
  49. float: left;
  50. width: 20px;
  51. text-align: center;
  52. left: 0;
  53. }
  54. .angle::after {
  55. content: "";
  56. display: inline-block;
  57. width: 0;
  58. height: 0;
  59. vertical-align: middle;
  60. border-top: solid 4px #333;
  61. border-left: solid 6px transparent;
  62. border-right: solid 6px transparent;
  63. }
  64. .angle.closed::after {
  65. border-left: solid 4px #333;
  66. border-top: solid 6px transparent;
  67. border-bottom: solid 6px transparent;
  68. }
  69. .light {
  70. color: #0595ff;
  71. }

</style>`
vue解析json数据
在前端接授到json后,使用JSON.parse(jsonObject)就可以解析!(jsonObject为json对象)

更多相关文章

  1. android(2)
  2. Android开发之InstanceState详解
  3. React Native 中文版(含最新Android章节)
  4. Android开发指南(37) —— Data Backup
  5. [Android] 提高ORMLite插入大量数据效率的解决方案
  6. Android(安卓)LCD(二):LCD常用接口原理篇
  7. Android短彩信数据库信息整理
  8. Android学习笔记_12_网络通信之从web获取资源数据到Android
  9. Android(安卓)开源组件无限循环ViewPager

随机推荐

  1. Android studio下gradle Robolectric单元
  2. 新书内容连载(3):Android SDK中常用命令行工
  3. [书目20101207]Google Android开发入门与
  4. android内存处理机制
  5. Android的异步(Thread、Handler、AsyncTas
  6. Android平台简介
  7. Maven开发Android指南 1 简介
  8. android 进程与线程 - 开发文档翻译 - 线
  9. android兼容小米xiaomi刘海屏解决方案
  10. android adb