简体   繁体   English

如何在 Javascript 中循环一个简单的 JSON object

[英]How to loop through a simple JSON object in Javascript

Okay, so i'm aware there are similar questions on this on the site BUT all I have seen have people with an array INSIDE an array.好的,所以我知道网站上有类似的问题,但我所看到的都是有人在数组里面有一个数组。 So I'm just looking for a bit of help on a simple one that I just can't seem to grasp.所以我只是在一个我似乎无法掌握的简单问题上寻找一些帮助。

The JSON is being generated from a PHP file and looks something like this when retrieved via AJAX: {'name': 'Derick', 'email': 'email@example.com'} . JSON 是从 PHP 文件生成的,当通过 AJAX: {'name': 'Derick', 'email': 'email@example.com'}

How can I loop through this response, using Javascript, to retrieve the value for, say, the 'name' key in the array?如何使用 Javascript 循环遍历此响应,以检索数组中“名称”键的值?

code snippet:代码片段:

response = {'name' : 'Derick', 'email' : 'email@example.com'};

If the response is only the given object, you can clearly get it by response['name'] .如果响应只是给定的 object,可以通过response['name']清楚的得到。

If the response that you recieve is an array of objects like the one you wrote:如果您收到的响应是一组对象,例如您编写的对象:

let result = [];
response.forEach(e => result.push(e['name']));

Should do what you want.应该做你想做的。

Your example code snippet is not an object array, but just an object.您的示例代码片段不是 object 数组,而只是 object。 You would not loop thru it to access the data.您不会循环通过它来访问数据。 But rather, access the data like this:而是像这样访问数据:

response.name and response.email

If your json object was an array, it would look something like this:如果您的 json object 是一个数组,它看起来像这样:

responses = [
    {
      'name' : 'Derick', 
      'email' : 'email@example.com'
    },
    {
      'name' : 'John', 
      'email' : 'johnsemail@example.com'
    }    
]

And you can loop through it such as:您可以循环遍历它,例如:

for (x of responses) {
  console.log(x.name + ' ' + x.email);
}

Okay, after trying all these suggestions, nothing worked.好的,在尝试了所有这些建议之后,没有任何效果。 Can't understand why.无法理解为什么。 So the solution that has worked for me is this, (You can also find it on W3Schools, under 'Javascript JSON parse() method).所以对我有用的解决方案是这样的,(您也可以在 W3Schools 上的 'Javascript JSON parse() 方法下找到它)。

JSON.parse(response, function (key, value) {
  if (key == "name") {
      console.log(value);
  }
});

So basically, the code parsed the response (even though my response should have come back parsed. Redundant, I know) to a javascript object and loops through said object to find they key-value pair you're looking for.所以基本上,代码将响应解析(即使我的响应应该已经解析。冗余,我知道)到 javascript object 并循环通过所述 object 寻找键值对。

This has worked for me although I have been unable, for the life of me, to deduce what may be wrong with simply using response.name or response['name'] to access them.这对我有用,尽管我一生都无法推断出仅使用 response.name 或 response['name'] 访问它们可能有什么问题。

Anyway, hope this helps.无论如何,希望这会有所帮助。 A big thank you to all who took time out of their day to reach out to help a junior dev.非常感谢所有抽出时间来帮助初级开发人员的人。

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

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