欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue如何實現(xiàn)Json格式數(shù)據(jù)展示

 更新時間:2022年04月07日 15:51:59   作者:A?brick?of?socialism  
這篇文章主要介紹了vue如何實現(xiàn)Json格式數(shù)據(jù)展示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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)簽包起來(這個要處理數(shù)據(jù)實現(xiàn)了,組件里高亮樣式可以根據(jù)需要自己修改)

<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數(shù)據(jù)

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • vue3+three.js實現(xiàn)疫情可視化功能

    vue3+three.js實現(xiàn)疫情可視化功能

    這篇文章主要介紹了vue3+three.js實現(xiàn)疫情可視化,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • Element實現(xiàn)表格嵌套、多個表格共用一個表頭的方法

    Element實現(xiàn)表格嵌套、多個表格共用一個表頭的方法

    這篇文章主要介紹了Element實現(xiàn)表格嵌套、多個表格共用一個表頭的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • vue keepAlive緩存清除問題案例詳解

    vue keepAlive緩存清除問題案例詳解

    這篇文章主要介紹了vue keepAlive緩存清除問題案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程

    vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程

    這篇文章主要介紹了vue+node+socket io實現(xiàn)多人互動并發(fā)布上線全流程,本文給大家提到了socket.io相關(guān)用法概覽及開發(fā)流程,需要的朋友可以參考下
    2021-09-09
  • 使用vux實現(xiàn)上拉刷新功能遇到的坑

    使用vux實現(xiàn)上拉刷新功能遇到的坑

    最近公司在研發(fā)app,選擇了基于Vue框架的vux組件庫,現(xiàn)總結(jié)在實現(xiàn)上拉刷新功能遇到的坑,感興趣的朋友參考下吧
    2018-02-02
  • 淺談Vue下使用百度地圖的簡易方法

    淺談Vue下使用百度地圖的簡易方法

    本篇文章主要介紹了淺談Vue下使用百度地圖的簡易方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Vue項目打包、合并及壓縮優(yōu)化網(wǎng)頁響應(yīng)速度

    Vue項目打包、合并及壓縮優(yōu)化網(wǎng)頁響應(yīng)速度

    網(wǎng)站頁面的響應(yīng)速度與用戶體驗息息相關(guān),直接影響到用戶是否愿意繼續(xù)訪問你的網(wǎng)站,所以這篇文章主要給大家介紹了關(guān)于Vue項目打包、合并及壓縮優(yōu)化網(wǎng)頁響應(yīng)速度的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式

    vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式

    這篇文章主要介紹了vue跳轉(zhuǎn)頁面打開新窗口,并攜帶與接收參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 基于vue hash模式微信分享#號的解決

    基于vue hash模式微信分享#號的解決

    這篇文章主要介紹了基于vue hash模式微信分享#號的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Vue?router應(yīng)用問題實戰(zhàn)記錄

    Vue?router應(yīng)用問題實戰(zhàn)記錄

    vue-router是vue.js官方的路由插件,他和vue.js是深度集成的適合構(gòu)建單頁面應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于Vue?router應(yīng)用問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04

最新評論