2013-05-13 2 views
1

아무도 나를 안내 할 수 있을까요? 일부 개체를 JSONArray로 구문 분석하려고합니다. 하지만 JSON 파일은 약간 복잡합니다. 나는 제목, 설명썸네일을 얻으려고안드로이드 JSON 파싱 getJSONObject

내 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(); 
} 

로그 캣 :

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) 
,363,210

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 개 배열 "항목"과 "volumeInfo"

+0

당신의 시도의 문제는 무엇을 사용 couldnt는? –

+1

"결과"키가있는 json 배열이 없습니다 – Blackbelt

+0

json에는 volumeninfo가 들어있는 항목이 들어 있습니다. 볼륨 정보에는 jsonarray라는 저자가 있습니다. – Blackbelt

답변

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); 
} 
+0

우수 답변. 감사 –