解析使用javascript作为JSON响应返回的java地图 [英] Parsing java map returned as JSON response using javascript
问题描述
我没有找到解决以下问题的具体答案。
I didn't found any specific answer which addresses the below question.
我通过AJAX POST请求获取了以下JSON响应。
I have the following JSON response fetched through an AJAX POST request.
{
"result":
{
"101010":["PRAVAT","SGSGSG","UKEMP5","UKENTD","WAUK01","MK87UK"],
"202020":["CORA1E","PSASAS","EDCRJS","USHC01","USDR06"],
............................
........................
"304050":["ERCDE2","DELT01","DECGKG","DEHC03","IS02","DEPI01"]
},
"status":"SUCCESS"
}
我想通过以下方式显示数据在javascript中使用循环。
我试过 for(var i = 0; i< response.result.length; i ++){
但是我无法这样做。
I want to display the data above data by using a loop in javascript.
I tried for ( var i = 0; i < response.result.length; i++) {
but I am not able to do so.
请帮助我如何使用javascript以上述JSON格式解析和显示我的数据。
Please help how can I parse and display my data in the above JSON format using javascript.
推荐答案
你拥有的是一个对象,而不是一个数组。只有数组具有length属性。迭代一个对象使用:
What you have is an object, not an array. Only arrays have the length property. To iterate over an object use:
$.post("yoururlhere", function(JSONData) {
var obj = $.parseJSON(JSONData);
if (obj.status.toLowerCase() === "success") {
for (var key in obj.result) {
if (obj.result.hasOwnProperty(key)) {
console.log(key + ': ' + obj.result[key]);
}
}
}
});
if(obj.result.hasOwnProperty(key))强制for忽略原型属性。如果你想查找它们,你就可以在Javascript中进行继承。
The if (obj.result.hasOwnProperty(key)) forces the for to ignore prototype properties. If you care to look it up they are the means you can do inheritance in Javascript.
这篇关于解析使用javascript作为JSON响应返回的java地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!