广告位联系
返回顶部
分享到

vue实现Json格式数据的介绍

JavaScript 来源:互联网 作者:秩名 发布时间:2022-04-07 19:50:49 人浏览
摘要

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

Json格式数据展示

vue的jsonViewer组件也很好用,在网上看到有大神自己写的组件(递归调用),觉得很好,稍作修改,记录一下

1

JSON.stringify(obj, null, 4)

可以美化json数据的显示 

<span class="light">要高亮的内容</span> 在json数据中,如果有需要高亮的内容,用以上标签包起来(这个要处理数据实现了,组件里高亮样式可以根据需要自己修改)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<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>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

<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>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

<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对象)


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/ltk1060669897/article/details/117290852
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计