简体   繁体   English

jQuery-如何使用每个循环遍历JSON

[英]JQuery - How to loop through JSON using each

I have cached the JSON returned from an Ajax call and need to loop through this to display it. 我已经缓存了从Ajax调用返回的JSON,需要循环遍历以显示它。 I get the error, 'Cannot read property 'title' of undefined'. 我收到错误消息“无法读取未定义的属性”标题”。 Can anyone help? 有人可以帮忙吗?

 $.each(cache['cat-'+cat], function(i, jd) {               
    var title= jd.title;  //issue is here
  )}

When I console.log(cache['cat-'+cat]) I get the below: 当我console.log(cache['cat-'+cat])我得到以下内容:

Object {
  date: "2016-07-28T15:08:03.596Z", 
  data: '[{"id":471,"title":"Lines and Calls","solution_areas":"lines-calls"}]'
}

When I console.log(jd) within the loop I get the below: 当我在循环内进行console.log(jd)时,我得到以下信息:

2016-07-28T15:13:14.553Z

if I use console.log(jd.data); 如果我使用console.log(jd.data); I get 我懂了

undefined 未定义

I have tried the below but they don't work either: 我尝试了以下方法,但它们也不起作用:

var title= jd.data.title;  
var title= jd.data[0].title;  

Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错?

The way you are currently using it, each is going to iterate over every property of the cache['cat-'+cat] object, of which there are two, date and data . 当前使用它的方式, each方法都将迭代cache['cat-'+cat]对象的每个属性,其中有两个datedata

So your anonymous function function(i, jd) will be called twice. 因此,您的匿名函数function(i, jd)将被调用两次。 The first time, jd will be the value of the date property (a string), the second time it will be the value of the data property (also a string, that happens to be formatted as JSON). 第一次, jd将是date属性的值(一个字符串),第二次它将是data属性的值(也是一个字符串,碰巧被格式化为JSON)。

The contents of data need to be parsed before they can be accessed as an object/array, and given that data is formatted as an array, I am guessing that you actually want to iterate over that. data的内容需要先解析,然后才能作为对象/数组进行访问,并且鉴于data已格式化为数组,我想您实际上是想对其进行迭代。 Given the example provided, I would change it to: 给定提供的示例,我将其更改为:

$.each(JSON.parse(cache['cat-'+cat].data), function(i, jd) {               
    var title= jd.title;  
 });

You aren't accessing it properly. 您没有正确访问它。 And since cache['cat-'+cat] is already the needed object, what's the purpose of $.each ? 并且由于cache['cat-'+cat]已经是需要的对象,因此$.each的目的是$.each Should be 应该

var title= JSON.parse(cache['cat-'+cat].data)[0].title;

(because the title is in the data , and the data is JSON). (因为标题位于data ,并且data为JSON)。

Demo: 演示:

 var obj = { date: "2016-07-28T15:08:03.596Z", data: '[{"id":471,"title":"Lines and Calls","solution_areas":"lines-calls"}]' }; var title= JSON.parse(obj.data)[0].title; console.log(title); 

Just need to understand what is JSON and what is STRING 只需了解什么是JSON和什么STRING

cache['cat-'+cat].data return a string that need to convert to JSON before pass to each loop: cache ['cat-'+ cat] .data返回一个字符串,该字符串在传递给每个循环之前需要转换为JSON:

var dataCacheReturned = cache['cat-'+cat];

var objCache = dataCacheReturned.data; //Its return a string 
objCache = JSON.parse(objCache); //Parse string to json

$.each(objCache, function(i, jd) {
  var title = jd.title; 
  console.log(title);
});

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

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