我正在尝试显示如下所示的JSON数据:
{
"response": {
"@attributes": {
"status": "ok",
"version": "12"
},
"label": {
"@attributes": {
"plid": "1234",
"name": "demo"
}
},
"page": {
"@attributes": {
"name": "bookinfo"
},
"count": 1,
"pages": "144",
"current_page": 1,
"isbn10": "0824828917",
"isbn13": "9780824828912",
"title": "Surf Science",
"author": "Tony Butt - Paul Russell - Rick Grigg",
"binding": "Paperback",
"msrp": 34,
"publisher": "University of Hawaii Press",
"published_date": "2004-07-01",
"edition": "2nd",
"rank": 176243,
"rating": 0,
"_amazon_item": null,
"image": "http:\/\/ecx.images-amazon.com\/images\/I\/51-dKe7tztL._SL75_.jpg"
}
}
}
我知道JQuery调用.getJSON可用于获取上面的对象,但我不知道如何从中提取数据,因此我可以在网页上动态显示它。有关如何使用JQuery / Javascript执行此操作的任何想法?例如,我如何获得作者或头衔?
我的代码如下(唯一不同的是,当我测试它时,我实际使用我的真实密钥):
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "http://api2.campusbooks.com/12/rest/search",
dataType: "json",
data: {
key : "[my_key]",
keywords : "calc",
timeout : "7",
format : "json"
},
success: function(jsonData) {
console.log("Success! " + jsonData);
},
error: function(jsonData) {
console.log("Error! " + jsonData);
}
});
</script>
</body>
</html>
firebug控制台中的打印件是:
Error! [object Object]
所以返回的对象只是[object Object]。 “错误!”部分仅来自我的代码。当我将所有变量放入1个url并转到它时,页面显示:
{"response":{"@attributes":{"status":"ok","version":"12"},"label":{"@attributes":{"plid":"[1234]","name":"[my_name]"}},"page":{"@attributes":{"name":"search"},"count":1000,"pages":100,"current_page":1,"results":{"book":[{"isbn10":"0538497394","isbn13":"9780538497398","title":"Essential Calculus: Early Transcendentals, Enhanced Edition (with Enhanced WebAssign with eBook Printed Access Card for Multi Term Math and Science)","author":"James Stewart","binding":"Hardcover","msrp":177.95,"pages":"944","publisher":"Brooks Cole","published_date":"2010-01-01","edition":"1","rank":12133,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/41%2Bk0odfWaL._SL75_.jpg"},{"isbn10":"1439058474","isbn13":"9781439058473","title":"Dosage Calculations (Available Titles 321 Calc!dosage Calculations Online)","author":"Gloria D. Pickar - Amy Pickar-Abernethy","binding":"Paperback","msrp":90.95,"pages":"720","publisher":"Delmar Cengage Learning","published_date":"2012-01-01","edition":"9","rank":2004,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/41TcBjzhPnL._SL75_.jpg"},{"isbn10":"0840065639","isbn13":"9780840065636","title":"Applied CALC (with Mathematics CourseMate with eBook Printed Access Card)","author":"Frank Wilson","binding":"Paperback","msrp":83.95,"pages":"256","publisher":"Brooks Cole","published_date":"2011-01-01","edition":"1","rank":514542,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/516p3a40S4L._SL75_.jpg"},{"isbn10":"1430231599","isbn13":"9781430231592","title":"Beginning OpenOffice Calc: From Setting Up Simple Spreadsheets to Business Forecasting","author":"Jacek Artymiak","binding":"Paperback","msrp":39.99,"pages":"136","publisher":"Apress","published_date":"2011-09-05","edition":"1","rank":682980,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51FFF95bq4L._SL75_.jpg"},{"isbn10":"0071488456","isbn13":"9780071488457","title":"Bob Miller's High School Calc for the Clueless - Honors and AP Calculus AB & BC (Bob Miller's Clueless Series)","author":"Bob Miller","binding":"Paperback","msrp":12.95,"pages":"246","publisher":"McGraw-Hill","published_date":"2007-08-14","edition":"1","rank":1121409,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51Adl-dr0cL._SL75_.jpg"},{"isbn10":"0821848178","isbn13":"9780821848173","title":"Riot at the Calc Exam and Other Mathematically Bent Stories","author":"Colin Adams","binding":"Paperback","msrp":32,"pages":"271","publisher":"American Mathematical Society","published_date":"2009-07-21","edition":"","rank":625803,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51Jvoh2ZQtL._SL75_.jpg"},{"isbn10":"111816170X","isbn13":"9781118161708","title":"Calculus II For Dummies","author":"Mark Zegarelli","binding":"Paperback","msrp":19.99,"pages":"384","publisher":"For Dummies","published_date":"2012-01-24","edition":"2","rank":16179,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51W8PJsg%2BZL._SL75_.jpg"},{"isbn10":"1847190979","isbn13":"9781847190970","title":"Learn OpenOffice.org Spreadsheet Macro Programming: OOoBasic and Calc automation: A fast and friendly tutorial to writing macros and spreadsheet applications","author":"Dr Mark Alexander Bain","binding":"Paperback","msrp":39.99,"pages":"220","publisher":"Packt Publishing","published_date":"2006-12-22","edition":"","rank":1118193,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51SOipRUqOL._SL75_.jpg"},{"isbn10":"0495014281","isbn13":"9780495014287","title":"Essential Calculus: Early Transcendentals (Stewart's Calculus Series)","author":"James Stewart","binding":"Hardcover","msrp":190.95,"pages":"912","publisher":"Brooks Cole","published_date":"2006-03-01","edition":"1","rank":1392,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/41T-md-t%2B6L._SL75_.jpg"},{"isbn10":"0764146939","isbn13":"9780764146930","title":"Barron's AP Calculus, 11th Edition","author":"David Bock M.S. - Shirley O. Hockett M.A.","binding":"Paperback","msrp":18.99,"pages":"696","publisher":"Barron's Educational Series","published_date":"2012-02-01","edition":"11","rank":3475,"rating":0,"image":"http:\/\/ecx.images-amazon.com\/images\/I\/51GENtM0pGL._SL75_.jpg"}]}}}}
使用的网址:
http://api2.campusbooks.com/12/rest/search?key=[my_key]&keywords=calc&timeout=7&format=json
答案 0 :(得分:3)
假设在显示时返回了JSON,这是一个例子:
$.getJSON("some/URL", function(jsonData) {
$("#titleDiv").html(jsonData.response.page.title);
// #titleDiv now holds the text "Surf Science"
// another way
$("#authorDiv").html(jsonData["response"]["page"]["author"]);
// #authorDiv now holds the text "Tony Butt - Paul Russell - Rick Grigg"
});
也许更好的方法是使用$.ajax
,因为这就是jQuery在幕后使用的方法。这样您就可以指定成功和错误处理函数,这将有助于调试您可能遇到的任何问题:
$.ajax({
type: "GET",
url: "http://api2.campusbooks.com/12/rest/bookinfo",
dataType: "json",
data: {
key : "[yourkey]",
isbn : "0538497815",
timeout : "6",
format : "json"
},
success: function(jsonData) {
console.log("Success! " + jsonData);
},
error: function(request, status, error) {
console.log("Error! " + request.responseText);
}
});
如果发生错误,您应该可以通过查看浏览器的JavaScript控制台找出它是什么。 顺便说一下,您可以像我上面那样指定参数,而不是将它们放入URL中。
还有许多开发人员工具可以帮助解决此类问题,例如Google Chrome的REST Console app或Firefox的RESTClient add-in。它们可以轻松输入您的请求参数并确定可能导致问题的原因。