y-json-view.vue 4.4 KB

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