简体   繁体   English

使用jquery遍历对象的json数组

[英]Iterating through a json array of objects using jquery

I know this may seem basic to some of you, but I am trying to iterate a json object. 我知道这对于某些人来说似乎很基本,但是我正在尝试迭代json对象。 I apologize as I seem to be having difficulty getting various examples to work. 很抱歉,我似乎很难使各种示例生效。 I've also read the jQuery documentation and the json examples listed there don't quite match my json structure. 我还阅读了jQuery文档,那里列出的json示例与我的json结构不太匹配。

Here's a link to my example which is displaying "null" in the console where I expected to see "1". 这是我的示例的链接,该示例在控制台中显示“ null”(我希望看到“ 1”)。 I'm just trying to print out the article_id element of each "node" in the json array. 我只是想打印出json数组中每个“节点”的article_id元素。

http://jsfiddle.net/zZjfj/1/ http://jsfiddle.net/zZjfj/1/

var json = [
    [{
        "article_id": 1,
            "article_title": "test",
            "article_content": "test1"
    }, {
        "article_id": 2,
            "article_title": "test2",
            "article_content": "this is a second test article"
    }]
];

$.each(json, function (arrayID, group) {
    console.log(group.article_id);
});

Your json is not an array of object, it is an array of array of object . 您的json不是对象数组,而是对象数组array

When you enter in your iteration, for each element, the arrayID is the index (0, 1, etc...) and the group is the sub array. 当您输入迭代时,对于每个元素, arrayID是索引(0、1等),而组是子数组。

To solve your problem, use json[0] instead of json in your code 要解决您的问题,请在代码中使用json [0]而不是json

$.each(json[0], function (arrayID, group) {
    console.log(group.article_id);
});

Your object 'json' is an array with 1 element, which is itself an array of 2 elements. 您的对象“ json”是一个包含1个元素的数组,它本身就是2个元素的数组。 If your change your example to: 如果您将示例更改为:

var json = [
    { "article_id": 1,
      "article_title": "test",
      "article_content": "test1" },
    { "article_id": 2,
      "article_title": "test2",
      "article_content": "this is a second test article" }
  ];

Then it produces the articles ids: 1,2. 然后生成文章ID:1,2。

If you need to iterate over a two dimensional array, then you can use a nested loop: 如果需要遍历二维数组,则可以使用嵌套循环:

$.each(json, function (idx1, entry) {
  $.each(entry, function (idx2, group) {
      console.log(group.article_id);
  });
});

You have an array nested inside of another array. 您有一个嵌套在另一个数组内部的数组。 You may want to take a look at how your JSON is being output, but in the meantime it's solved easily by another level of iteration: 您可能想看一下JSON的输出方式,但与此同时,它可以通过另一级别的迭代轻松解决:

$.each(json, function (arrayID, group) {
    $.each(group, function (arrayID, group) {
        console.log(group.article_id);
    });
});

http://jsfiddle.net/zZjfj/2/ http://jsfiddle.net/zZjfj/2/

I've created a script that will allow you to see paths to values in JSON as well: http://jsfiddle.net/ExplosionPIlls/zEBZr/1/ 我创建了一个脚本,该脚本也将允许您查看JSON中值的路径: http : //jsfiddle.net/ExplosionPIlls/zEBZr/1/

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

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