getJSON() 获取未定义的值
getJSON() getting undefined values
我试图从 JSON 文件中解析值 "title,author and ISBN" 并将它们存储在一个名为 availableTags
的数组中,但我只得到值 undefined
和我不知道问题出在哪里。
有什么建议吗?
我的代码:
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
availableTags[0] = data.title;
availableTags[1] = data.author;
availableTags[2] = data.ISBN;
alert(availableTags[0]);
});
});
这是JSON代码
[{"title":"the book","author":"Peter","ISBN":"632764"}]
您需要注意,您的数据变量实际上是一个数组。
您需要将代码更改为:
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
data = data[0];
availableTags[0] = data.title;
availableTags[1] = data.author;
availableTags[2] = data.ISBN;
alert(availableTags[0]);
});
});
您可能漏掉了括号。
这是一个包含一项的数组。
[{"title":"the book","author":"Peter","ISBN":"632764"}]
这是一件商品。
{"title":"the book","author":"Peter","ISBN":"632764"}
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
//use data[0] instead of data
var book = data[0];
availableTags[0] = book.title;
availableTags[1] = book.author;
availableTags[2] = book.ISBN;
alert(availableTags[0]);
});
});
好吧,有点晚了,但既然我写了一些东西,也许它对某人有帮助:
$(document).ready(function() {
var searchResults = [];
// Assuming data will be something like this
// data = [{"title":"the book","author":"Peter","ISBN":"632764"},{"title":"the other book","author":"Sebastian","ISBN":"123456"}];
$.getJSON("search.json", function(data) {
if (typeof data === 'object' && data.length > 0) {
searchResults = data;
console.info('Referenced to outside variable');
for (var amount = 0; amount < searchResults.length; amount++) {
var result = searchResults[amount];
// do something with each result here
console.group('Search-Result:');
console.log('Title: ', result.title);
console.log('Author: ', result.author);
console.log('ISBN: ', result.ISBN);
console.groupEnd();
}
} else {
console.error('Received wrong data, expecting data to be array');
}
});
});
我试图从 JSON 文件中解析值 "title,author and ISBN" 并将它们存储在一个名为 availableTags
的数组中,但我只得到值 undefined
和我不知道问题出在哪里。
有什么建议吗?
我的代码:
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
availableTags[0] = data.title;
availableTags[1] = data.author;
availableTags[2] = data.ISBN;
alert(availableTags[0]);
});
});
这是JSON代码
[{"title":"the book","author":"Peter","ISBN":"632764"}]
您需要注意,您的数据变量实际上是一个数组。
您需要将代码更改为:
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
data = data[0];
availableTags[0] = data.title;
availableTags[1] = data.author;
availableTags[2] = data.ISBN;
alert(availableTags[0]);
});
});
您可能漏掉了括号。
这是一个包含一项的数组。
[{"title":"the book","author":"Peter","ISBN":"632764"}]
这是一件商品。
{"title":"the book","author":"Peter","ISBN":"632764"}
$(document).ready(function () {
var availableTags = [];
$.getJSON( "search.json", function( data ) {
//use data[0] instead of data
var book = data[0];
availableTags[0] = book.title;
availableTags[1] = book.author;
availableTags[2] = book.ISBN;
alert(availableTags[0]);
});
});
好吧,有点晚了,但既然我写了一些东西,也许它对某人有帮助:
$(document).ready(function() {
var searchResults = [];
// Assuming data will be something like this
// data = [{"title":"the book","author":"Peter","ISBN":"632764"},{"title":"the other book","author":"Sebastian","ISBN":"123456"}];
$.getJSON("search.json", function(data) {
if (typeof data === 'object' && data.length > 0) {
searchResults = data;
console.info('Referenced to outside variable');
for (var amount = 0; amount < searchResults.length; amount++) {
var result = searchResults[amount];
// do something with each result here
console.group('Search-Result:');
console.log('Title: ', result.title);
console.log('Author: ', result.author);
console.log('ISBN: ', result.ISBN);
console.groupEnd();
}
} else {
console.error('Received wrong data, expecting data to be array');
}
});
});