y-json-view.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="jsonview">
  3. <view @click="toggleClose" :class="['angle', isShow ? 'closed' : '']" v-if="length"></view>
  4. <view class="first-line">
  5. <view v-if="jsonKey" class="json-key">"{{ jsonKey }}":</view>
  6. <view v-if="length" class="json-fold">{{ prefix }} {{ isShow ? '...' + subfix : '' }}</view>
  7. <view v-if="!length">{{ isArray ? '[]' : '{}' }}</view>
  8. </view>
  9. <view class="json-body" v-if="!isShow && length" v-for="(item, index) in items">
  10. <y-json-view v-if="item.isJSON" :key="index"
  11. :json="item.value" :jsonKey="item.key"
  12. :isLast="index === items.length - 1"></y-json-view>
  13. <view class="json-item" v-else>
  14. <view class="json-index" v-if="isArray">{{ index }}</view>
  15. <view class="json-key" v-else>{{ '"' + item.key + '"' }}</view>
  16. <view class="json-colon"><view>:</view></view>
  17. <view class="json-value">{{ item.value + (index === items.length - 1 ? '' : ',') }}</view>
  18. </view>
  19. </view>
  20. <view v-if="!isShow && length" class="last-line">
  21. <view>{{ subfix }}</view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'y-json-view',
  28. props: {
  29. json: {
  30. type: [Object, Array],
  31. default: null
  32. },
  33. jsonKey: {
  34. type: String,
  35. default: '' //第一个键
  36. },
  37. isLast: {
  38. type: Boolean,
  39. default: true
  40. },
  41. closed: {
  42. type: Boolean,
  43. default: false
  44. }
  45. },
  46. mounted() {
  47. this.isShow = this.closed;
  48. },
  49. watch: {
  50. closed:function() {
  51. this.isShow = this.closed;
  52. },
  53. },
  54. data() {
  55. return {
  56. isShow: true
  57. };
  58. },
  59. methods: {
  60. isObjectOrArray(source) {
  61. const type = Object.prototype.toString.call(source);
  62. const res = type === '[object Array]' || type === '[object Object]';
  63. return res;
  64. },
  65. toggleClose() {
  66. if (this.isShow) {
  67. this.isShow = false;
  68. } else {
  69. this.isShow = true;
  70. }
  71. }
  72. },
  73. computed: {
  74. isArray() {
  75. return Object.prototype.toString.call(this.json) === '[object Array]';
  76. },
  77. length() {
  78. //返回数组长度或对象的属性个数
  79. return this.isArray ? this.json.length : Object.keys(this.json).length;
  80. },
  81. subfix() {
  82. return (this.isArray ? ']' : '}') + (this.isLast ? '' : ',');
  83. },
  84. prefix() {
  85. return this.isArray ? '[' : '{';
  86. },
  87. items() {
  88. const json = this.json;
  89. if (this.isArray) {
  90. return this.json.map(item => {
  91. const isJSON = this.isObjectOrArray(item);
  92. //console.log(item)
  93. return {
  94. value: item, // isJSON ? item : JSON.stringify(item),
  95. isJSON, //相当于isJson:isJson
  96. key: ''
  97. };
  98. });
  99. } else {
  100. return Object.keys(json).map(key => {
  101. const item = json[key];
  102. const isJSON = this.isObjectOrArray(item);
  103. return {
  104. value: item, // isJSON ? item : JSON.stringify(item),
  105. isJSON,
  106. key
  107. };
  108. });
  109. }
  110. }
  111. }
  112. };
  113. </script>
  114. <style lang="scss">
  115. $tab:40upx;//缩进
  116. $font-size:16px;
  117. $colon-size:7px;//冒号两边的间隔
  118. $arrow-size:7px;
  119. $arrow-color:#333;
  120. $key-color:#c35e00;
  121. $index-color:#0000ff;
  122. $vaule-color:#42c800;
  123. .jsonview {
  124. line-height:$font-size * 1.62;
  125. font-size:$font-size;
  126. margin: 0;
  127. padding-left: $tab;
  128. white-space: nowrap;
  129. }
  130. .first-line {
  131. display: flex;
  132. flex-direction: row;
  133. justify-content: flex-start;
  134. align-items: center;
  135. text-align: center;
  136. .json-key {
  137. color:$key-color;
  138. }
  139. .json-fold {
  140. display: flex;
  141. flex-direction: row;
  142. justify-content: flex-start;
  143. }
  144. }
  145. .json-body {
  146. position: relative;
  147. padding: 0;
  148. margin: 0;
  149. display: flex;
  150. flex-direction: row;
  151. .json-item {
  152. display: flex;
  153. flex-direction: row;
  154. justify-content: flex-start;
  155. align-items: center;
  156. text-align: center;
  157. margin: 0;
  158. padding-left: $tab;
  159. .json-index {
  160. color:$index-color;
  161. }
  162. .json-colon {
  163. padding:0 $colon-size;
  164. }
  165. .json-key {
  166. color:$key-color;
  167. }
  168. .json-value {
  169. color:$vaule-color;
  170. }
  171. }
  172. }
  173. .last-line {
  174. display: flex;
  175. flex-direction: row;
  176. padding-left: 0;
  177. }
  178. .angle {
  179. position: absolute;
  180. display: block;
  181. left: 0;
  182. text-align: center;
  183. &::after {
  184. content: '';
  185. display: inline-block;
  186. width: 0;
  187. height: 0;
  188. vertical-align: middle;
  189. border-style: solid;
  190. border-color:transparent;
  191. border-top-color: $arrow-color;
  192. border-width:$arrow-size;
  193. }
  194. &.closed::after {
  195. border-style: solid;
  196. border-color:transparent;
  197. border-left-color: $arrow-color;
  198. border-width:$arrow-size;
  199. }
  200. //}
  201. }
  202. </style>