jQuery 使用javascript解析作为JSON响应返回的java映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16941148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:07:18  来源:igfitidea点击:

Parsing java map returned as JSON response using javascript

javascriptjqueryjson

提问by Pravat Panda

I didn't found any specific answer which addresses the below question.

我没有找到任何解决以下问题的具体答案。

I have the following JSON response fetched through an AJAX POST request.

我通过 AJAX POST 请求获取了以下 JSON 响应。

{
"result":
{
"101010":["PRAVAT","SGSGSG","UKEMP5","UKENTD","WAUK01","MK87UK"],
"202020":["CORA1E","PSASAS","EDCRJS","USHC01","USDR06"],
............................
........................
"304050":["ERCDE2","DELT01","DECGKG","DEHC03","IS02","DEPI01"]
},
"status":"SUCCESS"
}

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 中使用循环来显示数据上方的数据。我试过了,for ( var i = 0; i < response.result.length; i++) {但我做不到。

Please help how can I parse and display my data in the above JSON format using javascript.

请帮助我如何使用javascript以上述JSON格式解析和显示我的数据。

回答by Hoffmann

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

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.

if (obj.result.hasOwnProperty(key)) 强制 for 忽略原型属性。如果您想查找它,它们是您可以在 Javascript 中进行继承的方法。

回答by itsmejodie

Do you have it as an object or JSON?

你有它作为对象还是 JSON?

To convert the JSON to an object in jquery, use $.parseJSON().

要将 JSON 转换为 jquery 中的对象,请使用$.parseJSON().

EG. var obj = $.parseJSON(myJSONData);

例如。 var obj = $.parseJSON(myJSONData);

Once you have the object, you can loop through the keys using:

获得对象后,您可以使用以下方法遍历键:

for (var key in obj) {
    console.log(key + ': ' + obj[key]);
}

回答by MostafaR

You should parse it, You can use JSON.parse()or jQuery.parseJSON(). Have a look at this: Parse JSON in JavaScript?

您应该解析它,您可以使用JSON.parse()jQuery.parseJSON()。看看这个:在 JavaScript 中解析 JSON?

回答by Jedediah

As MostafaR said, you need to parse it into a javascript object first. When you get JSON from somewhere, javascript just considers it a string, so you won't be able to access it directly. Also, some older browsers don't have window.JSON, so you'll need to include the json2.js libraryif you're worried about support from older browsers.

正如 MostafaR 所说,您需要先将其解析为 javascript 对象。当您从某处获取 JSON 时,javascript 只会将其视为字符串,因此您将无法直接访问它。此外,一些较旧的浏览器没有 window.JSON,因此如果您担心旧浏览器的支持,则需要包含json2.js 库