迭代跟随JSON时出错

时间:2019-02-13 08:15:24

标签: javascript jquery ecmascript-6 ecmascript-5

我有以下json,但无法从json中获取结果

{"records":[{"Id":"EAAAAMQFNOLb3sYKmS2SByuEngKnNLcaGuDKpHIn1yo8Y-WC","PlateNo":"B108","ChassisNo":"8101108","AxleLoaded":null,"AxleSpacing":1,"ChangeInDimension":null,"FrontAxleLoadWeight":null,"RearSingleAxleLoadWeight":null,"NoSingleAxles":1,"NoTandomAxles":1,"NoMultiAxles":1,"NoTyresPerAxles":11,"MaxAxleLoadedLoad":1,"TyreLoadedLoad":1,"TyreWidth":1,"IsTrailer":true,"Cost":30,"CreatedOn":"\/Date(1549892412737)\/","CreatedById":"EAAAAMQFNOLb3sYKmS2SByuEngK94K-Aro6CPgaTtLl1wTsw","TirePressure":1,"LoadWeight":null,"UnloadedWeight":1,"Length":1,"Width":1,"Height":1,"LoadHeight":0,"RearTripleAxleLoaded":null,"LoadWidth":null}],"total":1}

我使用了以下jquery代码,但显示未定义。我也尝试使用$ .parseJSON,但无法正常工作 我已经将先前的json分配给了一个变量。

                   var mapdata = newVal;
                    alert(newVal);
                    $.each(mapdata, function (index, mapinfo) {

                        console.log(mapinfo.PlateNo);
                        alert(mapinfo.PlateNo);
                    });

1 个答案:

答案 0 :(得分:0)

由于您使用的是该格式,因此几乎缺少索引records

var data = {
  "records": [{
    "Id": "EAAAAMQFNOLb3sYKmS2SByuEngKnNLcaGuDKpHIn1yo8Y-WC",
    "PlateNo": "B108",
    "ChassisNo": "8101108",
    "AxleLoaded": null,
    "AxleSpacing": 1,
    "ChangeInDimension": null,
    "FrontAxleLoadWeight": null,
    "RearSingleAxleLoadWeight": null,
    "NoSingleAxles": 1,
    "NoTandomAxles": 1,
    "NoMultiAxles": 1,
    "NoTyresPerAxles": 11,
    "MaxAxleLoadedLoad": 1,
    "TyreLoadedLoad": 1,
    "TyreWidth": 1,
    "IsTrailer": true,
    "Cost": 30,
    "CreatedOn": "\\/Date(1549892412737)\\/",
    "CreatedById": "EAAAAMQFNOLb3sYKmS2SByuEngK94K-Aro6CPgaTtLl1wTsw",
    "TirePressure": 1,
    "LoadWeight": null,
    "UnloadedWeight": 1,
    "Length": 1,
    "Width": 1,
    "Height": 1,
    "LoadHeight": 0,
    "RearTripleAxleLoaded": null,
    "LoadWidth": null
  }],
  "total": 1
}

$.each(data.records, function(index, mapinfo) {

  console.log(mapinfo.PlateNo);
  alert(mapinfo.PlateNo);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>