Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't convert json string to object in Vue.js and javascript

im trying to convert this json string to object, but i can't, the console this erro:

Uncaught SyntaxError: Unexpected end of JSON input

at JSON.parse (<anonymous>)
at Vue$3.buildResults (rolList.js:422)
at Vue$3.boundFn [as buildResults] (vue.js:141)
at Vue$3.mounted (rolList.js:418)
at callHook (vue.js:2768)
at Vue$3.Vue._mount (vue.js:2630)
at Vue$3.$mount (vue.js:6186)
at Vue$3.$mount (vue.js:8557)
at Vue$3.Vue._init (vue.js:3389)
at new Vue$3 (vue.js:3437)
The json string is this:

{"modulos":{"1":{"name":" Usuarios","paginas":{"1":{"name":" Listado Usuarios","facultades":{"1":{"name":" ver"}}}}}}}

The java script code is this:

  resultsLi = new Vue({
    el: '#result-list',
    data: {
      resultls: "",
      json: []
    },
    mounted: function () {
      this.buildResults();
    },
    methods: {
      buildResults: function(event) {
        this.json = JSON.parse(this.resultls);
        console.log(this.resultls);

      }
    },
    watch: {
      resultls: function(val, oldVal){
        this.buildResults();
      }
    },
    delimiters: ['${', '}']
  });
like image 530
Brayan Caldera Avatar asked Mar 26 '26 17:03

Brayan Caldera


1 Answers

You are getting that error because on initial load, your resultls is an empty string. You can change it to be a blanket empty json so it will parse properly and will be updated in watch.

resultsLi = new Vue({
  el: '#result-list',
  data: {
    resultls: "{}",
    json: []
  },
  mounted: function () {
   .....
}
like image 103
serdar.sanri Avatar answered Mar 28 '26 06:03

serdar.sanri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!