简体   繁体   中英

iron ajax: how to parse array of json object from the success response?

i am making iron ajax call to server i am getting the response as array of object of array 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}]}]

in the success response when i am calling success method and printing the data i am getting [object,object] in console, how should i parse this object of array of object 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>

parsed the json with three for loops, so my success function goes:-

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

Le me know for any simpler solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM