如何在 javascript 中循环遍历 JSON 关联数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6047460/
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-23 20:01:35  来源:igfitidea点击:

How to loop through an JSON associative array in javascript?

javascriptjqueryjsonloopsassociative-array

提问by ssin

I'm getting a JSON response from the server and i have to loop through the array in javascript and get the values. But I cant seem to loop throught it.

我从服务器收到一个 JSON 响应,我必须在 javascript 中遍历数组并获取值。但我似乎无法遍历它。

The JSON response of the array looks like this:

数组的 JSON 响应如下所示:

{
???"1": "Schools",
???"20": "Profiles",
???"31": "Statistics",
???"44": "Messages",
???"50": "Contacts"
}

I just want to loop through it to get the ID and Name and populate some values on the page.

我只想遍历它以获取 ID 和名称并在页面上填充一些值。

I have tried:

我试过了:

$.each(response, function(key, value) {
    alert(key + ' ' + value);
});

// and 

for (var key in response) {
    alert(key + ' ' + response[key]);
}

But neither give the right values.

但两者都没有给出正确的值。

Thanks in advance for any help.

在此先感谢您的帮助。

Reply: Hi, The response I'm getting with the second loop is:

回复:嗨,我在第二个循环中得到的响应是:

0 {
1 "
2 1
3 "
4 :
5 "
6 S

etc etc

等等等等

So that means its going through the whole response as a string and spliting it as key/value.

所以这意味着它作为一个字符串遍历整个响应并将其拆分为键/值。

Thanks

谢谢

回答by Juan Mendes

Your problem is that you are not parsing the JSON string. Therefore, your foreach is going through the characters in the JSON string.

您的问题是您没有解析 JSON 字符串。因此,您的 foreach 正在遍历 JSON 字符串中的字符。

// If you are using jQuery.ajax, you can just set dataType to 'json' 
// and the following line will be done for you
var obj  = jQuery.parseJSON( response );
// Now the two will work
$.each(obj, function(key, value) {
    alert(key + ' ' + value);
});


for (var key in obj) {
    alert(key + ' ' + response[key]);
}

回答by Miguel Rodrigues

var response = {"1":"Schools","20":"Profiles","31":"Statistics","44":"Messages","50":"Contacts"};

for (var i in response) {
    console.log(i + ' ' + response[i]);
}

Works just fine, how are you getting your response var?

工作得很好,你如何得到你的响应变量?

回答by trinity

You don't need to do like that, dealing with string is a boring job. You can make a object through the response. 1:json = eval(xmlHttp.responseText);

你不需要那样做,处理字符串是一项无聊的工作。您可以通过响应创建一个对象。1:json = eval(xmlHttp.responseText);

but this is unsafe in some degree.

但这在某种程度上是不安全的。

  1. json = JSON.parse(xmlHttp.responseText, function(key,value){// can do some other stuff here.});
  1. json = JSON.parse(xmlHttp.responseText, function(key,value){// can do some other stuff here.});

then you can operate the variable as a normal object like this obj.a or obj["a"].

然后你可以像这样 obj.a 或 obj["a"] 那样将变量作为一个普通对象来操作。

May this will help you.

愿这对你有帮助。

回答by Aravindan R

http://jsfiddle.net/sG5sF/

http://jsfiddle.net/sG5sF/

jQuery.eachworks fine. So is for-eachloop

jQuery.each工作正常。for-each循环也是如此

http://jsfiddle.net/TfjrS/

http://jsfiddle.net/TfjrS/

Both of them work as they should. You might have errors in other parts of your code. Is the responsevariable set correctly to the JSON object given in your question? Are you checking the response statusCode? it should be 200 for a successful response?

它们都按其应有的方式工作。您的代码的其他部分可能有错误。是response变量设置正确地在你的问题中给出的JSON对象?您是否正在检查响应状态代码?成功响应应该是200?

回答by Vivin Paliath

You can use for-inconstruct in pure Javascript. Of course, you need to be careful that you're only looking at the object's own properties (libraries like Prototype tend to pollute):

您可以for-in在纯 Javascript 中使用构造。当然,您需要小心,您只查看对象自身的属性(Prototype 之类的库往往会造成污染):

for(var key in response) {
    if(response.hasOwnProperty(key)) {
       ...
    }
}

EDIT

编辑

Are you using jQuery.ajax? What's the dataTypevalue? It should be json. This might be why your response is being interpreted as a string. Also, when you console.logresponse, does it show up as a string or an object?

你在用jQuery.ajax吗?有什么dataType价值?应该是json。这可能就是您的响应被解释为字符串的原因。另外,当您console.log响应时,它显示为字符串还是对象?

回答by Shamoon

Consider looking at How can I parse a JavaScript object with jQueryfor a possible answer.

考虑查看如何使用 jQuery 解析 JavaScript 对象以获得可能的答案。