2014-09-25 106 views
0

我有一组json数据已经通过url输出。数据有不同的级别,我需要访问一个级别,然后在jquery循环中使用这些数据来操作一些函数。如何从json数据的输出中获取特定的数据?

JSON的URL输出数据是这样的:

{ 
"id": 32, 
"title": "sascas", 
"synopsis": "<p>cxcxcx</p>\r\n", 
"body": "<p>cxccxxc</p>\r\n", 
"created_at": "2014-09-25T14:12:06.345Z", 
"updated_at": "2014-09-25T14:12:06.345Z", 
"author": { 
"name": "A test author", 
"biography": "/system/authors/avatars/000/000/024/original/photo.JPG?1411472688", 
"id": 24 
}, 
"schools": { 
"schools": [ 
{ 
"id": 5, 
"school_name": "Another london school Inner Circle Regent's Park, London", 
"website": null, 
"book_id": 32, 
"created_at": "2014-09-25T14:12:06.413Z", 
"updated_at": "2014-09-25T14:12:06.413Z" 
}, 
{ 
"id": 6, 
"school_name": "city of london school 107 Queen Victoria St London", 
"website": null, 
"book_id": 32, 
"created_at": "2014-09-25T14:12:06.417Z", 
"updated_at": "2014-09-25T14:12:06.417Z" 
} 
] 
} 
} 

我基本上是试图访问只在一个js函数中这个循环的学校数据。目前该功能使用每个循环并将标记添加到地图上。我想知道的是,我的功能只能获取学校数据吗?这是我为它js函数:

var initMap = function() { 

# The url that is generated in the html for me to grab 
var info = $('#map').data('book-title'); 
$.get(info, {}, function(response) { 
setMarkers(map, response); 
# some other js stuff for map here inside the $get 
}, 'json'); 

} 
在setmarkers功能

然后,我有这样的:在控制台

function setMarkers(map, json) { 
jQuery.each(json, function (index, city) { 
var school = city['schools']; 
console.log(school); 
}); 
} 

在输出我得到这个:

[Object, Object]0: Object book_id: 32 created_at: "2014-09-25T14:12:06.413Z"id: 5school_name: "Another london school Inner Circle Regent's Park, London" updated_at: "2014-09-25T14:12:06.413Z" website: null__proto__: Object__define Getter__: function __defineGetter__() { [native code] }__defineSetter__: function __defineSetter__() { [native code] }__lookupGetter__: function __lookupGetter__() { [native code] }__lookupSetter__: function __lookupSetter__() { [native code] }constructor: function Object() { [native code] }hasOwnProperty: function hasOwnProperty() { [native code] }isPrototypeOf: function isPrototypeOf() { [native code] }propertyIsEnumerable: function propertyIsEnumerable() { [native code] }toLocaleString: function toLocaleString() { [native code] }toString: function toString() { [native code] }valueOf: function valueOf() { [native code] }get __proto__: function __proto__() { [native code] }set __proto__: function __proto__() { [native code] }1: Objectbook_id: 32created_at: "2014-09-25T14:12:06.417Z"id: 6school_name: "city of london school 107 Queen Victoria St London"updated_at: "2014-09-25T14:12:06.417Z"website: null__proto__: Objectlength: 2__proto__: Array[0] 

我需要访问该阵列中的特定数据,即school_name。这可能吗?

+2

当然。 '城市['学校']'是一系列学校,所以只需重复一遍即可。请参阅[访问/进程(嵌套)对象,数组或JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – 2014-09-25 16:43:01

+1

Felix是一个向导。如果他提到你的文章,请阅读文章。研究它。明白它。真正的幸福将是你的。 – gibberish 2014-09-25 16:50:12

+0

我可以使用'console.log(data ['school_name']);'获得一所学校的名字,但这已经在.each循环中了,所以我做了'jQuery.each(json ['schools']),函数(索引,城市)',它通过数组并获取学校数据。我只需要在循环内部获取每个学校的名称就可以了。 – 2014-09-25 17:34:53

回答

0
console.dir(school) 

- 可以让您更清楚地了解您所看到的内容。

正如另一张海报所说,由于学校是一系列学校,你最好打赌是迭代。您可以通过jQuery或JavaScript中的标准循环来使用您当前在其他地方使用的.each方法。

for (var i = 0; i < schools.length; i++) { 
    //Party with stuff in here 
} 

祝你好运!

0

这jQuery.each将打印数据的学校:

jQuery.each(response.schools.schools, function (i, school) { 
     console.log("id " + school["id"] + ", school_name: " + school["school_name"] ); 
    }); 

或替代

jQuery.each(response.schools.schools, function (i, school) { 
    console.log("id " + school.id + ", school_name: " + school.school_name ); 
}); 

打印:

ID 5,school_name:另一个伦敦学校金环摄政公园,伦敦

id 6,school_name:伦敦城市学校107 Queen Victoria St Lon don

注意我的fiddle没有JSON请求。

+1

我可以在我的jQuery.each中使用它吗? – 2014-09-25 18:40:08

+0

@Mdunbavan是的,它看起来像像jQuery.each和For循环做同样的事情,这实际上是我第一次使用jQuery.each方法,它确实非常方便:-) – jyrkim 2014-09-26 09:04:59