vue如何实现Json格式数据展示
目录
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;
}
.json-view {
position: relative;
display: block;
width: 100%;
height: 100%;
/* white-space: nowrap; */
padding: 0 20px;
box-sizing: border-box;
line-height: 30px;
}
.json-note {
color: #909399;
}
.json-key {
color: #333333;
}
.json-value {
color: #333333;
}
.json-item {
margin: 0;
padding-left: 20px;
}
.first-line {
padding: 0;
margin: 0;
}
.json-body {
position: relative;
padding: 0;
margin: 0;
}
.json-body .body-line {
position: absolute;
height: 100%;
width: 0;
border-left: dashed 1px #bbb;
top: 0;
left: 2px;
}
.last-line {
padding: 0;
margin: 0;
}
.angle {
position: absolute;
display: block;
cursor: pointer;
float: left;
width: 20px;
text-align: center;
left: 0;
}
.angle::after {
content: "";
display: inline-block;
width: 0;
height: 0;
vertical-align: middle;
border-top: solid 4px #333;
border-left: solid 6px transparent;
border-right: solid 6px transparent;
}
.angle.closed::after {
border-left: solid 4px #333;
border-top: solid 6px transparent;
border-bottom: solid 6px transparent;
}
.light {
color: #0595ff;
}
</style>`
vue解析json数据
在前端接授到json后,使用JSON.parse(jsonObject)就可以解析!(jsonObject为json对象)
更多相关文章
- android(2)
- Android开发之InstanceState详解
- React Native 中文版(含最新Android章节)
- Android开发指南(37) —— Data Backup
- [Android] 提高ORMLite插入大量数据效率的解决方案
- Android(安卓)LCD(二):LCD常用接口原理篇
- Android短彩信数据库信息整理
- Android学习笔记_12_网络通信之从web获取资源数据到Android
- Android(安卓)开源组件无限循环ViewPager