溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue怎么實(shí)現(xiàn)Json格式數(shù)據(jù)展示

發(fā)布時(shí)間:2022-04-08 12:56:59 來源:億速云 閱讀:1234 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“vue怎么實(shí)現(xiàn)Json格式數(shù)據(jù)展示”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“vue怎么實(shí)現(xiàn)Json格式數(shù)據(jù)展示”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

Json格式數(shù)據(jù)展示

vue的jsonViewer組件也很好用,在網(wǎng)上看到有大神自己寫的組件(遞歸調(diào)用),覺得很好,稍作修改,記錄一下

JSON.stringify(obj, null, 4)

可以美化json數(shù)據(jù)的顯示 

<span class="light">要高亮的內(nèi)容</span> 在json數(shù)據(jù)中,如果有需要高亮的內(nèi)容,用以上標(biāo)簽包起來(這個(gè)要處理數(shù)據(jù)實(shí)現(xiàn)了,組件里高亮樣式可以根據(jù)需要自己修改)

<template>
    <div class="bgView">
        <div :class="['json-view', length ? 'closeable' : '']" :>
            <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" >
                       {{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數(shù)據(jù)

在前端接授到j(luò)son后,使用JSON.parse(jsonObject)就可以解析?。╦sonObject為json對象)

讀到這里,這篇“vue怎么實(shí)現(xiàn)Json格式數(shù)據(jù)展示”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI