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