Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show json result with vue.js

Hi i am try to show json file result with vue.js the target is that result will be showed on value.

this is my code:

data () {
  return {
    fetchData: function () {

      var self = this;
      self .$http.get( "/api/casetotalactivation", function( data ) {
        self.items = data;
      });
    },

    statsCards: [
      {
        type: 'warning',
        icon: 'ti-server',
        title: 'Cases',
        value: this.items,
        footerText: 'Updated now',
        footerIcon: 'ti-reload'
      }


    ],
like image 672
Sofiane Avatar asked Jan 22 '18 08:01

Sofiane


2 Answers

use this code:

<div id="vueapp">
  <textarea v-model="jsonstr" rows="8" cols="40"></textarea>
  <pre>{{ jsonstr | pretty }}</pre>
</div>

and JS:

new Vue({
  el: '#vueapp',
  data: {
    jsonstr: '{"id":1,"name":"A green door","price":12.50,"tags":["home","green"]}'
  },
  filters: {
    pretty: function(value) {
      return JSON.stringify(JSON.parse(value), null, 2);
    }
  }
})
like image 115
Behnam Mohammadi Avatar answered Sep 30 '22 18:09

Behnam Mohammadi


HTML and JS have this built into the language. Try...

<pre>{{ yourObject }}</pre>

This gives the default indent, to specify a custom indent provide it as the third argument to JSON.stringify(...).

// replace 2 with '\t' to do tab indentation 
<pre>{{ JSON.stringify(yourObject, null, 2) }}</pre>

If you're outside of Vue then using some combo of the above snipped will do.

like image 43
Caveman Avatar answered Sep 30 '22 19:09

Caveman