iron ajax:如何从成功响应中解析json对象的数组?

时间:2017-09-14 16:05:13

标签: javascript ajax polymer-1.0 iron-ajax

我正在对服务器进行铁ajax调用我将响应作为数组对象的数组作为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}]}]

在成功响应中,当我调用成功方法并打印数据时,我在控制台中获取[对象,对象],如何在成功方法中解析对象数组的这个对象?

<!-- 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>

1 个答案:

答案 0 :(得分:0)

用三个for循环解析了json,所以我的成功函数是: -

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);
      }
    }
  }
}

让我知道任何更简单的解决方案