有人可以指导我吗?我正在尝试将一些对象解析为我的JSONArray。但我的JSON文件有点复杂。我正在尝试获取标题,描述和缩略图
我的JSON文件
{
"query": {
"count": 1,
"created": "2013-05-13T17:22:38Z",
"lang": "en-US",
"results": {
"json": {
"kind": "books#volumes",
"totalItems": "1145",
"items": [
{
"kind": "books#volume",
"id": "2_3AjlqS2DMC",
"etag": "t6birOoou5k",
"selfLink": "https://www.googleapis.com/books/v1/volumes/2_3AjlqS2DMC",
"volumeInfo": {
"title": "Introducing Android Development with Ice Cream Sandwich",
"authors": [
"Shane Conder",
"Lauren Darcey"
],
"publisher": "Addison-Wesley Professional",
"publishedDate": "2011-10-31",
"description": "The Android platform continues to aggressively grow in market share against competing mobile platforms, such as Apple iOS and BlackBerry. Android's latest major platform update, Android 4.0, frequently called by its code-name, Ice Cream Sandwich or just ICS merges the smartphone-centric Android 2.3.x (Gingerbread) and the tablet-centric Android 3.x (Honeycomb) platform editions into a single SDK for all smart-devices, be they phones, tablets, televisions, or toasters. This short e-book provides an overview from the authors on the importance of Ice Cream Sandwich as well as key preview content from the upcoming book, \"Android Wireless Application Development, Third Edition, Volume I.\" This preview content provides some essential references, updated for Android SDK 4.0, for those interested in jumping into Android application development at this exciting time. To use this e-book most effectively, you need to download the Android development SDK and tools, install them on your development machine, and configure them using the development environment of your choice. You can find instructions for installing and configuring your computer for Android software development on the Android Developer website at http://d.android.com/sdk/.",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0132947595"
},
{
"type": "ISBN_13",
"identifier": "9780132947596"
}
],
"pageCount": "96",
"printType": "BOOK",
"categories": "Computers",
"averageRating": "3.0",
"ratingsCount": "4",
"contentVersion": "0.1.0.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://bks7.books.google.com/books?id=2_3AjlqS2DMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://bks7.books.google.com/books?id=2_3AjlqS2DMC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.com/books?id=2_3AjlqS2DMC&printsec=frontcover&dq=android&hl=&cd=2&source=gbs_api",
"infoLink": "http://books.google.com/books?id=2_3AjlqS2DMC&dq=android&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.com/books/about/Introducing_Android_Development_with_Ice.html?hl=&id=2_3AjlqS2DMC"
},
"saleInfo": {
"country": "MY",
"saleability": "NOT_FOR_SALE",
"isEbook": "false"
},
"accessInfo": {
"country": "MY",
"viewability": "PARTIAL",
"embeddable": "true",
"publicDomain": "false",
"textToSpeechPermission": "ALLOWED_FOR_ACCESSIBILITY",
"epub": {
"isAvailable": "true",
"acsTokenLink": "http://books.google.com/books/download/Introducing_Android_Development_with_Ice-sample-epub.acsm?id=2_3AjlqS2DMC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"pdf": {
"isAvailable": "true",
"acsTokenLink": "http://books.google.com/books/download/Introducing_Android_Development_with_Ice-sample-pdf.acsm?id=2_3AjlqS2DMC&format=pdf&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"webReaderLink": "http://books.google.com/books/reader?id=2_3AjlqS2DMC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE"
},
"searchInfo": {
"textSnippet": "This short e-book provides an overview from the authors on the importance of Ice Cream Sandwich as well as key preview content from the upcoming book, "Android Wireless Application Development, Third Edition, Volume I." This preview content ..."
}
},
{
"kind": "books#volume",
"id": "ASmO8r7i93sC",
"etag": "tXvdPFo+cKk",
"selfLink": "https://www.googleapis.com/books/v1/volumes/ASmO8r7i93sC",
"volumeInfo": {
"title": "Android Essentials",
"authors": "Chris Haseman",
"publisher": "Apress",
"publishedDate": "2008-08-01",
"description": "Android Essentials is a no–frills, no–nonsense, code–centric run through the guts of application development on Google’s Mobile OS. This book uses the development of a sample application to work through topics, focusing on giving developers the essential tools and examples required to make viable commercial applications work. Covering the entirety of the Android catalog in less than 150 pages is simply impossible. Instead, this book focuses on just four main topics: the application life cycle and OS integration, user interface, location–based services, and networking. Thorough, complete, and useful work on the nuts and bolts of application development in Android Example driven and practically minded A tool for hobbyists and professionals who want to create production–quality applications What you’ll learn Understand how an Android application functions and communicates with the handset that hosts it. Comprehend the complexities of timers, services, and multimedia playback. Create and display a rich mix of custom–rendered screens and tailored Android widgets. Understand how location–based services are becoming more and more important in the mobile world. See how to use Google’s powerful Map tool. Explore the intricacies of network connectivity in the mobile world. Who this book is for This book is for professional software engineers looking to move their ideas and applications into the mobile space with Android. The author assumes the reader has a passable understanding of Java. They should be able to write classes and handle basic inheritance structures. This book also targets hobbyist developers looking to negotiate the complex minefield of mobile software. Table of Contents Introduction The Application User Interface Location, Location, Location Taking Android Out for a Walk Tying on a Bow",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "143021063X"
},
{
"type": "ISBN_13",
"identifier": "9781430210634"
}
],
"pageCount": "100",
"printType": "BOOK",
"categories": "Computers",
"averageRating": "1.5",
"ratingsCount": "3",
"contentVersion": "preview-1.0.0",
"imageLinks": {
"smallThumbnail": "http://bks7.books.google.com/books?id=ASmO8r7i93sC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://bks7.books.google.com/books?id=ASmO8r7i93sC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.com/books?id=ASmO8r7i93sC&printsec=frontcover&dq=android&hl=&cd=3&source=gbs_api",
"infoLink": "http://books.google.com/books?id=ASmO8r7i93sC&dq=android&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.com/books/about/Android_Essentials.html?hl=&id=ASmO8r7i93sC"
},
"saleInfo": {
"country": "MY",
"saleability": "NOT_FOR_SALE",
"isEbook": "false"
},
"accessInfo": {
"country": "MY",
"viewability": "PARTIAL",
"embeddable": "true",
"publicDomain": "false",
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": "false"
},
"pdf": {
"isAvailable": "true"
},
"webReaderLink": "http://books.google.com/books/reader?id=ASmO8r7i93sC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE"
},
"searchInfo": {
"textSnippet": "Android Essentials is a no–frills, no–nonsense, code–centric run through the guts of application development on Google’s Mobile OS. This book uses the development of a sample application to work through topics, focusing on giving ..."
}
}
]
}
}
}
}
我的尝试
try {
JSONObject jsonobject = JSONfunctions.getJSONfromURL("MYURL");
JSONObject json_query = jsonobject.getJSONObject("query");
JSONObject json_results = json_query.getJSONObject("results");
JSONObject json_json_result = json_results.getJSONObject("json");
JSONArray jsonarray = json_json_result.getJSONArray("results");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
json_json_result = jsonarray.getJSONObject(i);
map.put("title", json_json_result.getString("title"));
map.put("description", json_json_result.getString("description"));
map.put("thumbnail", json_json_result.getString("thumbnail"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
LogCat:
05-13 15:46:50.762: E/Error(2268): No value for results
05-13 15:46:50.762: W/System.err(2268): org.json.JSONException: No value for results
05-13 15:46:50.762: W/System.err(2268): at org.json.JSONObject.get(JSONObject.java:354)
05-13 15:46:50.762: W/System.err(2268): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
NEW EDIT
try {
JSONObject json_data = JSONfunctions.getJSONfromURL(url);
JSONObject json_query = json_data.getJSONObject("query");
JSONObject json_results = json_query.getJSONObject("results");
JSONObject json_json_result = json_results.getJSONObject("json");
JSONArray json_result = json_json_result.getJSONArray("items");
//JSONArray json_result = json_json_resultitem.getJSONArray("volumeInfo");
for (int i = 0; i < json_result.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = json_result.getJSONObject(i);
//map.put("title", c.getString("title").toString());
map.put("description", c.optString("title"));
arraylist.add(map);
}
当我尝试抓取标题或描述时,我得到NULL。我不能使用2个数组“items”和“volumeInfo”
答案 0 :(得分:3)
试试这个,
JSONObject json_data = JSONfunctions.getJSONfromURL(url);
JSONObject json_query = json_data.getJSONObject("query");
JSONObject json_results = json_query.getJSONObject("results");
JSONObject json_json_result = json_results.getJSONObject("json");
JSONArray json_result = json_json_result.getJSONArray("items");
//JSONArray json_result = json_json_resultitem.getJSONArray("volumeInfo");
for (int i = 0; i < json_result.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = json_result.getJSONObject(i);
JSONObject vo = c.getJSONObject("volumeInfo");
map.put("title", vo.optString("title"));
map.put("description", vo.optString("description"));
JSONObject il = vo.getJSONObject("imageLinks");
map.put("thumbnail", il.optString("thumbnail"));
arraylist.add(map);
}