What is the best way to handle JSON data in vue.js?

I am facing an issue with vue.js.

I want to create a settings page on the server program that has XML data.

I believe it can be achieved by following these steps :

  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

However, I am unable to read JSON as the received data contains '@' symbol.

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

 - - - - 

I am having trouble reading the @type attribute in my 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>

Thank you for taking the time to read this.

Answer №1

I'm not very experienced with Vue, but after looking at your code I believe you may be encountering issues with objects. If the value name is not a valid JavaScript variable name, you won't be able to access it using dot notation. In that case, you can access the value 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>

Answer №2

After reviewing the code, it appears that there is an issue with console.log(nodes.length); not working as expected. This is likely due to the structure of the object being like this:

"Nodes": {
  "Node": [
    {
    "@type": "development",

- - - - 
It seems that the key "Nodes" is an object containing "Node" as an array. To access the length of this array, you should use console.log(nodes.node.length) instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring z-indices in event bubbling

JSFiddle: https://jsfiddle.net/uLap7yeq/19/ Issue Let's examine a scenario where there are two elements, canvas and div, positioned in the same location using CSS. The div has a higher z-index compared to the canvas, but how can we make sure events ...

How can an object be utilized within the image tag's src attribute?

Is it possible to use an object in the img src attribute? // HTML <img src= "item.img" ...

Trouble with Displaying 3rd Level JQuery Dropdown Menu

Currently working on implementing a 3 level dropdown feature. I have been using a third-party responsive menu in Opencart and it's been working well. You can see a demo of it here: Unfortunately, Opencart does not natively support 3 level categories, ...

iOS hover menu behavior: Close dropdown when the same element is tapped again on iPads

The navigation bar features categories such as: politics, economy, education Upon clicking on these categories, a dropdown menu appears with locations like: asia, europe, North America What I am looking for is this: If the user clicks (tabs) or hovers (o ...

Unable to decipher the mysterious map of nothingness

I am currently working on a GET method in Node.js. My goal is to fetch data using the GET request and then use the MAP function to gather all the names into an array. However, I encountered the following error: /root/server.js:21 ...

Linking Redux to the highest level of my application is not functioning as expected

I have been attempting to troubleshoot this code for quite some time now, but I am struggling to identify the issue at hand. My main goal is to establish a connection between my top-level application and the redux store. However, every time I try, the stor ...

Why is it that PowerShell cannot execute Angular commands?

Recently, I started diving into Angular and encountered an issue using PowerShell in Windows. Every time I run an angular command like: ng new new-app or ng serve I keep getting this error message: ng : File C:\Users\< username >\ ...

Unable to utilize a third setState function due to the error message indicating an excessive number of re-renders

My current challenge involves setting an initial state for my Hook. I have a clickable element that changes the state between Decreasing and Increasing upon click, and this part is functioning properly. However, I encounter an issue when attempting to defi ...

Delete a filename in Internet Explorer with the power of JavaScript and jQuery

In my attempts to clear the file input field in IE using $('#files').val('');, I found that it was not effective. $('#uploadimgdiv').html(''); var fil1 = document.getElementById("files"); $('#fil1').val(&a ...

Protection of Angular expressions

I have been following the PhoneCat tutorial for AngularJS and found it very helpful up until step 6 where links are generated dynamically from angular expressions: http://localhost:8000/app/{{phone.imageUrl}} Although the tutorial mentions that ngSrc pre ...

Exploring the process of iterating through a JSON post response and displaying its contents within image divs

After receiving a JSON post response, I am looking to iterate over the data and display it in image divs. Can anyone provide guidance on how to achieve this using JavaScript? Thank you. Here is the JavaScript code that handles the JSON post response: cor ...

Utilizing the Twitter API 1.1 to retrieve a list of tweets

As I work on updating my CMS component, I am incorporating integration with the Twitter API to fetch and showcase a list of tweets related to a user or search query. I have chosen to utilize the Twitter Restful API v1.1 as the 1.0 version is set to be disc ...

Uploading a file with AngularJS and storing it in a database

I have been attempting to implement ngFileUpload in order to upload images and store them in a database – specifically, a mongoLab database that accepts JSON objects which can be posted using this syntax: $http.post('myMongoName/myDb/myCollection/ ...

Extracting information from $router within a Vue component

I need to manage the status of users who access my system through a URL generated by a QR Code. In my routes.ts file, I'm checking if the user is logged in and then redirecting them accordingly. if (store.getters.isLogged && to.path === 'QRCode& ...

Interpreting an undefined HTTP GET request within a Node.js server

I am encountering an issue within my Node.js application. When I send an http get request through an ajax call on the client-side, the server-side does not recognize the request data and returns an "undefined" error message. This problem is puzzling to me ...

The MaterialUI table pagination feature is experiencing an issue where the "Next" button is

Every time I attempt to search for a record with a lower value, such as 6, 14, 19, or 20, the Next button does not become active. However, when dealing with larger records, it functions perfectly fine. I am uncertain about what mistake I might be making. ...

"Encountering an issue with the parameter 'undefined' in the .find()

Recently, I've been facing some challenges while using the .find() method within a vuex getter. I initialized a store with a list under state (pages) // state.js const state = { pages: [{id:1, content:"Page 1"}, {id:2, content:"Page 2"}] }; In my ...

The life cycle of the request/response object in Express.js when using callbacks

Feel free to correct me if this question has already been asked. (I've done as much research as I can handle before asking) I'm really trying to wrap my head around the life cycle of request and response objects. Take a look at the following co ...

What are the steps for combining two constants with the JSON format of "const = [ { } ]"?

Here is the initial code that I would like to merge with another section. const sections = [ { title: section1_title, rows: [{ title: section1_option01, rowId: "sec1_option01", ...

Enhancing a v-data-table with Vuetify's editing features

Hey there, I've been trying to customize my v-data-table to better fit my needs in terms of rows and language. I've successfully edited some fields, but I'm struggling to figure out how to change the "of" in "1-50 of 300." I've been go ...