这个问题在这里已有答案:
我跟随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);
});
因为你有这种格式,几乎缺少索引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>