简体   繁体   English

如何使用 jquery.each() 遍历 json 数组

[英]How to loop through json array using jquery.each()

I was trying to show my json data using jquery.each().我试图使用 jquery.each() 显示我的 json 数据。 now this is the json现在这是json

 var json = 
  [
    {"id":"1","tagName":"apple"},
    {"id":"2","tagName":"orange"
  }]

then i can show it然后我可以展示它

 $.each(json, function(idx, obj) {
    alert(obj.tagName);
 });

But my json is但我的 json 是

 {
  "Message": "Success",
  "Code": "200",
  "Payload": [
    {
     "year": "2015",
     "month": "6",
     "fileCount": "985",
     "totalFileSize": "2820"
    },
    {
     "year": "2015",
     "month": "7",
     "fileCount": "15347",
     "totalFileSize": "66549"
    }
   ]
  }

Now I need to read the data inside the Payload.现在我需要读取 Payload 中的数据。 please help me with请帮我

Very simply.很简单。 Payload is property of the object that you have provided, which can be accessed like this: json.Payload . Payload是您提供的对象的属性,可以这样访问: json.Payload So you should pass it as the first argument to $.each() method.所以你应该把它作为第一个参数传递给$.each()方法。 Here you go:干得好:

$.each(json.Payload, function(idx, obj) {
     console.log(obj);
 });

http://jsfiddle.net/2wxwkxbd/ http://jsfiddle.net/2wxwkxbd/

You can also use你也可以使用

$.map(array,function(obj,index){
     console.log(obj)
});

for iterating through an array用于遍历数组

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

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