21

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


    ],
Sofiane
  • 271
  • 1
  • 5
  • 12

6 Answers6

45

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);
    }
  }
})
Behnam
  • 6,244
  • 1
  • 39
  • 36
  • Because my `value` was a Vue-wrapped object, I actually had to reverse the method order to call `JSON.parse` after calling `JSON.stringify` on the value like so: `JSON.parse(JSON.stringify(value, null, 2))` Suggested at https://stackoverflow.com/a/58098164/4531155 – RSmithlal Apr 16 '21 at 15:23
26

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 snippet will do.

Caveman
  • 2,527
  • 1
  • 17
  • 18
20

just use <pre>

<pre>{{json}}</pre>
Balaji
  • 9,657
  • 5
  • 47
  • 47
1

Here's one way to display JSON data, using Vue:

  • Display a stringified json object inside a <textarea> using v-model
  • Display object properties with <li v-for="">
<template>
  <div class="hello">
    <textarea v-model="listDataString" rows="20" cols="80"></textarea>
    <ul id="items">
      <li v-for="(item, index) in listData" :key="index">
        {{ `${item.text} [${item.id}]` }}
      </li>
    </ul>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "RenderList",
  props: {
    msg: String,
  },
  data() {
    return {
      listDataString: String,
      listData: [], // placeholder
    };
  },
  mounted() {
    axios
      .get("=== [API_ENDPOINT] ===")
      .then((response) => {
        this.listDataString = JSON.stringify(response.data, null, "\t");
        this.listData = response.data;
        console.log(this.listDataString);
        return response; // multiline arrow function must return
      });
  },
};
</script>

screenshot

Kalnode
  • 9,386
  • 3
  • 34
  • 62
Ted
  • 240
  • 2
  • 12
0

If the /api is only on the dev server you can create a vue.config.js file in the app root folder.

module.exports = {
  devServer: {
    before: function(app, server) {
      app.get('/api', function(req, res) {
        const result = [{
          type: 'warning',
          icon: 'ti-server',
          title: 'Cases',
          value: this.items,
          footerText: 'Updated now',
          footerIcon: 'ti-reload'}];
        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end(JSON.stringify(result));
      });
    }
  }
}

With this files when I run npm run serve, I get the json object when navigating to /api and my regular app otherwise.

Pascal Ganaye
  • 1,174
  • 12
  • 28
0

Just use this:

<pre v-html="JSON.stringify(objectJs, null, 2)"></pre>
Richelly Italo
  • 1,109
  • 10
  • 9