我正在尝试使用jQuery获取JSON值,但是我的代码没有像我期望的那样工作。你能否就我如何解决问题向你提出建议?
// JSON response:
[{
"private": "8044553.0"
}, {
"governmentdocs": "98952.0"
}, {
"officialdocs": "5577356.0"
}]
$.each($.parseJSON(data), function(idx, obj) {
privatedocs = obj.private;
alert(obj.private);
alert(obj.officialdocs);
alert(obj.governmentdocs);
});
我得到的值为8044553.0
,然后是undefined
,然后是5577356.0
。为什么会这样显示?
答案 0 :(得分:0)
您的 JSON 存在问题:[{"private":"8044553.0"}, {"governmentdocs":"98952.0"}, {"officialdocs":"5577356.0"}]
您的 JSON 应该:
'[{"private":"8044553.0", "governmentdocs":"98952.0", "officialdocs":"5577356.0"}]'
以下是工作示例:
var data ='[{"private":"8044553.0", "governmentdocs":"98952.0", "officialdocs":"5577356.0"}]';
$.each($.parseJSON(data), function(idx, obj) {
privatedocs=obj.private;
alert(obj.private);
alert(obj.officialdocs);
alert(obj.governmentdocs);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>