Demystifying Iron Ajax: Unraveling the process of parsing an array of JSON objects from a successful

When making an AJAX call to the server, I receive a response in the form of an array of objects as JSON.

[{"dms":[{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":37.8688,"longitude":-144.2093,"toolType":1},{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":31.8688,"longitude":-115.2093,"toolType":1}],"gyro":[{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":37.8688,"longitude":-144.2093,"toolType":1},{"serialNo":"EG0022","status":"running","firmwareStatus":"ok","latitude":31.8688,"longitude":-115.2093,"toolType":1}]}]

After calling the success method and printing the data, I see [object, object] in the console. How can I parse this object of array of objects in the success method?

<!-- Import Polymer -->
<script src="../../bower_components/webcomponentsjs/webcomponentslite.js"></script>

<link rel="import" href="../../bower_components/polymer/polymer.html"/>
<link rel="import" href="../../bower_components/paper-item/paperitem.html">
<link rel="import" href="../../bower_components/paper-listbox/paperlistbox.html">
<link rel="import" href="../../bower_components/paper-dropdown-menu/paperdropdown-menu.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">

<!-- Seed app components -->
<dom-module id="tool-bar">
  <template>
    <iron-ajax
      id="ajax"
      url=" rest URL"
      params='{"type":"all"}'
      handle-as="json"
      content-type="application/json"
      method="GET"
      on-response="mapResponse"
      debounce-duration="3000">
    </iron-ajax>
  </template>
  <script>
    Polymer({
      is : 'tool-bar',
      properties: {
        gyrodata: {
          type: Array
        }
      },

      mapResponse: function (data) {
        console.log(data.detail.response); //[Object,Object]
      }
    });
  </script>
</dom-module>

Answer №1

After parsing the JSON data, I utilized three nested for loops in my success function:

mapResponse: function (data) {
  var response = data.detail.response;
  for(var i = 0;i < response.length; ++i) {
    for (var key in response[i]) {
      for (var j = 0; j < response[i][key].length; j++) {
        console.log(response[i][key][j].serialNo);
        console.log(response[i][key][j].status);
        console.log(response[i][key][j].firmwareStatus);
      }
    }
  }
}

If anyone knows of a simpler solution, please let me know.

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

Error: Failed to decode audio content due to a DOMException that was caught in the promise

Encountering an issue with the decodeAudioData method while utilizing the Web Audio API for playback in Chrome (works seamlessly in Firefox)- Sending the audio buffer, which has been recorded by the media recorder, back from the server. Server-side wss ...

How can I design a form that resembles the sign-in form used by Google?

Currently, I am in the process of creating a contact form for a website that is inspired by the design of Google's material sign-in form. I have successfully implemented an effect where clicking on the input field causes the label to change its posit ...

Unique rephrased text: "Varied wrapping styles in both

Feeling frustrated with a seemingly commonplace issue. Despite the thousands of times it has been asked before, I can't seem to find an answer on Google. I wanted to neatly display my text within one of my cards, but so far I've only achieved th ...

The access-control-allow-origin header is failing to receive updates

I need help figuring out how to manually set the Access-origin header. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge& ...

Initiate and terminate server using supertest

I've developed a server class that looks like this: import express, { Request, Response } from 'express'; export default class Server { server: any; exp: any; constructor() { this.exp = express(); this.exp.get('/' ...

How can I streamline a kendo UI MVC project by eliminating unnecessary components?

After switching my MVC 5 project to utilize Kendo UI, I've noticed a significant increase in the number of files being used. Since there is no need for supporting other cultures at the moment, can I confidently delete the files within the messages an ...

Persistent navigation once fullscreen banner is completed

I have a full screen header on my one-page website. Below the hero section is the navigation element, which I want to be fixed after scrolling past the height of the full screen. Here's what I currently have in terms of code. HTML: <div id="hero" ...

Reconfigure the Node application using Express to seamlessly access modules through route files

I recently created a basic app using npm express. After generating the app.js file, I attempted to add new modules but encountered an issue where I couldn't access them in the route files. For example, when trying to utilize the 'request' mo ...

Is there a callback or event that can be used to ensure that getComputedStyle() returns the actual width and height values?

Currently, I find myself in a situation where I need to wait for an image to load before obtaining its computed height. This information is crucial as it allows me to adjust the yellow color selector accordingly. Question: The process of setting the yello ...

Connect the jQuery UI droppable feature to the Lift ajax handler

I'm currently developing a web application using Scala / Lift and I want to enhance user experience by adding drag and drop functionality. However, I am unsure how to integrate jQuery with Lift for this purpose. At the moment, I have a draggable div ...

Update the SQL database by transferring star ratings from a JSP file

Utilizing JSP to showcase information obtained from user queries, I have implemented a system where contextual data and the query itself are saved in a MySQL database using JDBC each time a new query is input. To enhance user interaction, I wanted to incor ...

Finding elements in an array based on a specific string contained within a property

I am currently working on filtering JSON data to specifically search for job roles that begin with a particular string. The structure of the JSON is as follows : "periods": [ { "periodName": "Week1", "teamName": "Tango", ...

How to retrieve a property with a colon in JavaScript object

After parsing XML into JSON with xml2js, I found elements with colons like this: obj = { "data:example":"smthing"}; Is there a way to directly access these elements in JSON? Any tips or tricks? Thank you! ...

What steps can be taken to avoid the appearance of the JavaScript prompt "Leaving site"?

Hi there, I'm currently trying to find a way to remove the Javascript prompt/confirm message that asks "Do you want to leave this site?" like shown in this link: The issue I am facing is that when a modal opens and the user clicks on "YES", it redire ...

Moving information from one controller to another, or the process of converting a controller into a service

Is there a way for me to transfer information from one controller to another? Or can I create a service from a controller? Specifically, I am looking to retrieve coordinates and store them in an object along with other variables. When I try to inject depen ...

transferring information from the controller to the model

On my website, I have a piece of code that sends data to Ajax and performs well when adding items to the cart. public function addToCart() { $lapId = $this->input->post('id_lap'); $numItems = $this- ...

Why does xpath insist on choosing spaces instead of actual elements?

Here is a list of countries in XML format: <countries> <country> <code>CA</code> <name>Canada</name> </country> ... etc... </countries> I am looking to extract and loop through these nodes, so ...

What steps can be taken to stop Internet Explorer from caching Ajax requests in Angular2 using TypeScript?

Imagine a situation where a user has 10 points. When they click a button, an ajax call is made to the server to update the user's points after they have been used. The server should send back 9 points, which is functioning correctly on all browsers ex ...

What is the best way to retrieve dynamic content from a .txt file and have it displayed on multiple HTML pages as they are loaded dynamically?

I have a file named 'm_data.txt' stored on the server that contains a continuous total of 77 (for instance). The total gets updated via a push.php page that writes to the .txt file. <!DOCTYPE html> <html> <head> <title> ...

How to Retrieve the Absolute Index of the Parent Column Using jQuery Datatables

I recently developed a custom column filtering plugin for datatables, but I've encountered a minor issue. Within each column footer, I have added text inputs, and now I am trying to capture their indexes on keyup event to use them for filtering. In ...