javascript - How to parse json in vue.js? - Stack Overflow

I have a trouble in vue.js.I wanna make the settings page on server program that has a xml data.I think

I have a trouble in vue.js.

I wanna make the settings page on server program that has a xml data.

I think, That will work if done as follows :

  1. server | parse xml to json
  2. client | get json and read json
  3. client | edit json
  4. client | push json to server
  5. server | parse json to xml
  6. server | save xml

But I can't read json because received json data has '@'.

"?xml": {
   "@version": "1.0",
   "@encoding": "utf-8"
},
"Nodes": {
  "Node": [
  {
    "@type": "development",

 - - - - 

I can't read @type attribute in script..

// script

<script>
var node = new Vue({
  el: '#Nodes',
  data: {
  json: null
},
filters: {
  checkNum: function(value) {
  var result = value;
    if (value < 10) {
      var result = "0" + value;
    }
  return result;
  },
}
})

$(document).ready(function(){
  console.log("ready");
  getNodes();
  setTimeInterval();
});

function getNodes() {
  var $url ="http://localhost:21004/nodes/current/get/json"
  $.ajax({
  url: $url,
  type: 'get',
  success: function (data) {
    console.log(data);
    console.log(data.Nodes[0].type);
    node.json = data;
  },
  error: function(err) {
    console.log(err);
  }
})
}

function setTimeInterval () {
  setInterval(function() {
  getNodes();
},3000)
}

</script>

// HTML

<ul id="Nodes">
  <li v-for="node in json.Nodes">
    {{ node.@type }}
    {{ node.@group }}
    {{ node.@name }}
  <li v-for="item in node.Items">
    {{ node.@name }}
    {{ node.@defaultValue }}
    {{ node.@expression }}
    {{ node.@format }}
  </li>
  </li>
</ul>

Thanks to read.

I have a trouble in vue.js.

I wanna make the settings page on server program that has a xml data.

I think, That will work if done as follows :

  1. server | parse xml to json
  2. client | get json and read json
  3. client | edit json
  4. client | push json to server
  5. server | parse json to xml
  6. server | save xml

But I can't read json because received json data has '@'.

"?xml": {
   "@version": "1.0",
   "@encoding": "utf-8"
},
"Nodes": {
  "Node": [
  {
    "@type": "development",

 - - - - 

I can't read @type attribute in script..

// script

<script>
var node = new Vue({
  el: '#Nodes',
  data: {
  json: null
},
filters: {
  checkNum: function(value) {
  var result = value;
    if (value < 10) {
      var result = "0" + value;
    }
  return result;
  },
}
})

$(document).ready(function(){
  console.log("ready");
  getNodes();
  setTimeInterval();
});

function getNodes() {
  var $url ="http://localhost:21004/nodes/current/get/json"
  $.ajax({
  url: $url,
  type: 'get',
  success: function (data) {
    console.log(data);
    console.log(data.Nodes[0].type);
    node.json = data;
  },
  error: function(err) {
    console.log(err);
  }
})
}

function setTimeInterval () {
  setInterval(function() {
  getNodes();
},3000)
}

</script>

// HTML

<ul id="Nodes">
  <li v-for="node in json.Nodes">
    {{ node.@type }}
    {{ node.@group }}
    {{ node.@name }}
  <li v-for="item in node.Items">
    {{ node.@name }}
    {{ node.@defaultValue }}
    {{ node.@expression }}
    {{ node.@format }}
  </li>
  </li>
</ul>

Thanks to read.

Share asked Feb 3, 2018 at 3:46 JaydenJayden 551 gold badge2 silver badges7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

I am not good with vue. But from you code I can say you have issue with objects. You cann't access to value with dot notation if value name is invalid javascript variable name. Use it like this.

<ul id="Nodes">
 <li v-for="node in json.Nodes">
  {{ node['@type'] }}
  {{ node['@group'] }}
  {{ node['@name'] }}
 <li v-for="item in node.Items">
  {{ item['@name'] }}
  {{ item['@defaultValue'] }}
  {{ item.['@expression'] }}
  {{ item.['@format'] }}
 </li>
 </li>
</ul>
console.log(nodes.length);  This won't work because your object 
structure is like following :
"Nodes": {
"Node": [
{
  "@type": "development",

  - - - - 
where in **Nodes is an object which has a key as "Node" and this "Node" 
key is an array** so console.log(nodes.node.length) should work.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074556a4609753.html

相关推荐

  • javascript - How to parse json in vue.js? - Stack Overflow

    I have a trouble in vue.js.I wanna make the settings page on server program that has a xml data.I think

    14小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信