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

Ravaging a JSON array in Delphi

This piece of code is causing a memory leak. Can you suggest the correct approach to tackle this issue? JSONArrayObject := TJSONArray.Create; try JSONArrayObject := TJSONObject.ParseJSONVal ...

EmberJS - how to register a precompiled handlebars template

In my EmberJS application, I have precompiled all of my handlebars templates so that they are loaded as pure Javascript files. However, I am encountering an issue where these precompiled templates are not being added to the Ember container as expected. In ...

Listening for Checkbox Changes in VueJS

Currently, I have a lengthy list of checkboxes and I want to group two sets of three checkboxes to act as radio buttons. Ignoring the UX aspect for now, what is the most effective way to achieve this functionality? Each checkbox corresponds to a property ...

What is the best way for me to collect messages submitted through a form on my website?

After completing my website using HTML, CSS, and JavaScript, I added a form with inputs for name, email, and message at the bottom of the page. Now, I am trying to figure out how to receive the information submitted by visitors in my email. ...

Exploring the mechanics of callbacks in jQuery's Ajax functionality

Greetings fellow developers! I've embarked on a new programming project with the firm intention of writing cleaner and more easily maintainable code than ever before. However, I have encountered my old nemesis - jQuery AJAX returns. The last time I t ...

Utilizing JQuery to display JSON data on a data table using Ajax

Seeking guidance on jQuery, I am diving into a new datatable venture with preloaded data. Upon searching, the goal is to update the table by removing existing data and displaying only the search results. My attempt so far includes: app.common.genericAjaxC ...

Unexpected behavior in AngularJS: HTTP post failing to send object correctly

I am currently facing an issue where the changes made to a table row through a form in AngularJS are not being correctly applied to the object, resulting in a 500 error when attempting to post on my development environment. You can view the code here Edi ...

How to Align Text at the Center of a Line in Three.js

Exploring What I Possess. https://i.sstatic.net/nAtmp.png Setting My Goals: https://i.sstatic.net/svcxa.png Addressing My Queries: In the realm of three.js, how can I transform position x and y into browser coordinates to perfectly align text in th ...

Locating a user by their ID within a collection in Meteor can lead to some unexpected behavior

I have a requirement where I only want to allow users to insert a document if their email is verified. In an attempt to achieve this, I wrote the following code snippet. Events.allow({ insert: function (userId, doc) { var user = Meteor.users.f ...

Rect cannot be resized using mouse events

I am currently working on resizing the rectangle inside the SVG using mouse events. To achieve this, I have created another circle shape at the right bottom edge of the rectangle and implemented resize events on that shape. However, I'm facing an issu ...

Rails may encounter issues with the format of parameters in ajax requests at times

Something strange is happening with my form: The form looks like this: <form id="my-form" method="post" action="/password_reset/update/<%= @user.perishable_token %>" charset="utf-8" autocomplete="off"> <input type="password" name="use ...

Trouble in displaying Android ListView while fetching data from JSON server

Currently, I am facing an issue with an Android app that is using a ListView to display data fetched from a JSON server. The view contains three fields: IMAGE, NAME, and DESCRIPTION. Despite confirming that the data is retrieved from the server and set in ...

The image displayed by the @vercel/og API route is not appearing correctly

I am currently facing an issue with my Next.js app hosted on Vercel Edge while trying to set up the Vercel/og package. You can find more information about it here: https://vercel.com/docs/concepts/functions/edge-functions/og-image-generation Upon loading ...

What's the best method for uploading a file to AWS S3: POST or PUT requests?

Could you please provide insights on the advantages and disadvantages of utilizing POST versus PUT requests for uploading a file to Amazon Web Services S3? Although I have come across some relevant discussions on platforms like StackOverflow, such as this ...

Get canvas picture through JS Jquery

I've been attempting to save a canvas image to my desktop using this code: <script type="text/javascript"> var canvas; $(document).ready(function() { if ($('#designs-wrapper').length) { $('.design').eac ...

Invoking a REST API synchronously using JavaScript

I am just starting out with JavaScript and the npm ecosystem. I am attempting to upload data to my REST service using a POST call. The data is being fetched from a CSV file, which is going well so far. For each line of data that is fetched, I convert it as ...

Swift - JSON decoding error: "Was supposed to decode an array of any type, but instead found a dictionary."

Hello there! I have a question here that's at the root of my learning journey. I'm delving into SwiftUI and currently experimenting with data fetching from an API to store it in an array. At the moment, I have two essential files in place: First ...

Encountering an Issue Opening pom.xml (Maven) in Java on Ubuntu 16.04

After installing Eclipse from the Ubuntu software and Java via the terminal using: sudo apt-get install default-jdk I encountered a problem where I couldn't add Maven from within Eclipse unless I ran it as root. Although I can now convert projects ...

Node.js error: Attempting to set property '' on an undefined object not allowed

I encountered an issue while attempting to update an array within a model. Even though the usagePlan is defined before the update, the error below keeps getting thrown. customer.usagePlan.toolUsage.tools.push(aNewToolObject); customer.updateAttribute(&apo ...

Utilizing markers for gaming in a data table with Vue and Vuetify

I am struggling to retrieve the games of teams from two objects with arrays in Vue. One object contains headers for a data table, while the other object contains status information. Despite my efforts, I have not been successful in fetching the game detail ...