我有一组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
。这可能吗?
当然。 '城市['学校']'是一系列学校,所以只需重复一遍即可。请参阅[访问/进程(嵌套)对象,数组或JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – 2014-09-25 16:43:01
Felix是一个向导。如果他提到你的文章,请阅读文章。研究它。明白它。真正的幸福将是你的。 – gibberish 2014-09-25 16:50:12
我可以使用'console.log(data ['school_name']);'获得一所学校的名字,但这已经在.each循环中了,所以我做了'jQuery.each(json ['schools']),函数(索引,城市)',它通过数组并获取学校数据。我只需要在循环内部获取每个学校的名称就可以了。 – 2014-09-25 17:34:53