如何从json数据的输出中获取特定数据?

时间:2014-09-25 16:40:06

标签: javascript jquery ajax json

我有一组通过url输出的json数据。数据有不同的级别,我需要访问一个级别,然后在jquery循环中使用该数据来操作某些函数。

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 个答案:

答案 0 :(得分:0)

console.dir(school)

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

正如另一张海报所说,由于学校是一系列学校,你最好的办法就是重复学习。你可以使用你目前在其他地方使用的.each方法,通过jQuery或javascript中的循环标准。

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

祝你好运!

答案 1 :(得分: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:另一所伦敦学校Inner Circle Regent's Park,London

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

注意我的fiddle没有JSON请求。