简体   繁体   English

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

[英]How to loop through an JSON associative array in javascript?

I'm getting a JSON response from the server and i have to loop through the array in javascript and get the values. 我从服务器获得JSON响应,我必须在javascript中循环遍历数组并获取值。 But I cant seem to loop throught it. 但我似乎无法循环。

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和Name并在页面上填充一些值。

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 谢谢

Your problem is that you are not parsing the JSON string. 您的问题是您没有解析JSON字符串。 Therefore, your foreach is going through the characters in the JSON string. 因此,您的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]);
}
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? 工作得很好,你怎么得到你的回应var?

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

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. 愿这对你有所帮助。

http://jsfiddle.net/sG5sF/ http://jsfiddle.net/sG5sF/

jQuery.each works fine. jQuery.each工作正常。 So is for-each loop 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 response variable set correctly to the JSON object given in your question? response变量是否正确设置为问题中给出的JSON对象? Are you checking the response statusCode? 你在检查响应statusCode吗? it should be 200 for a successful response? 成功回应应该是200?

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

You can use for-in construct in pure Javascript. 您可以在纯Javascript中使用for-in构造。 Of course, you need to be careful that you're only looking at the object's own properties (libraries like Prototype tend to pollute): 当然,您需要注意的是,您只关注对象自己的属性(像Prototype这样的库会污染):

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

EDIT 编辑

Are you using jQuery.ajax ? 你在用jQuery.ajax吗? What's the dataType value? 什么是dataType值? It should be json . 它应该是json This might be why your response is being interpreted as a string. 这可能就是您的响应被解释为字符串的原因。 Also, when you console.log response, does it show up as a string or an object? 另外,当你使用console.log响应时,它是否显示为字符串或对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM