2014-04-10 40 views
2

我學習JSON解析在java.I've JSON字符串解析JSON字符串,在這裏它是如何用Java

{"responseData": { 
"results": [ 
    { 
    "GsearchResultClass": "GimageSearch", 
    "width": "450", 
    "height": "450", 
    "imageId": "Yt3TRC1vxzhazM", 
    "tbWidth": "127", 
    "tbHeight": "127", 
    "unescapedUrl": "http://www.touchnote.com/files/assets/STAN009.jpg", 
    "url": "http://www.touchnote.com/files/assets/STAN009.jpg", 
    "visibleUrl": "www.touchnote.com", 
    "title": "Touchnote - Personalised \u003cb\u003eFuzzy Monkey\u003c/b\u003e greeting cards design by Dan \u003cb\u003e...\u003c/b\u003e", 
    "titleNoFormatting": "Touchnote - Personalised Fuzzy Monkey greeting cards design by Dan ...", 
    "originalContextUrl": "http://www.touchnote.com/photo/card-design/Fuzzy+Monkey", 
    "content": "Card Design \u003cb\u003eFuzzy Monkey\u003c/b\u003e", 
    "contentNoFormatting": "Card Design Fuzzy Monkey", 
    "tbUrl": "http://images.google.com/images?q\u003dtbn:Yt3TRC1vxzhazM:www.touchnote.com/files/assets/STAN009.jpg" 
    }, 
    { 
    "GsearchResultClass": "GimageSearch", 
    "width": "640", 
    "height": "480", 
    "imageId": "c6093fGTdNvKOM", 
    "tbWidth": "137", 
    "tbHeight": "103", 
    "unescapedUrl": "http://stuff.fuzzymonkeyphotography.com/front_page/NEW_NEW_logo3_480px.jpg", 
    "url": "http://stuff.fuzzymonkeyphotography.com/front_page/NEW_NEW_logo3_480px.jpg", 
    "visibleUrl": "www.fuzzymonkeyphotography.com", 
    "title": "\u003cb\u003eFuzzy Monkey\u003c/b\u003e Photography", 
    "titleNoFormatting": "Fuzzy Monkey Photography", 
    "originalContextUrl": "http://www.fuzzymonkeyphotography.com/", 
    "content": "Welcome to \u003cb\u003eFuzzy Monkey\u003c/b\u003e", 
    "contentNoFormatting": "Welcome to Fuzzy Monkey", 
    "tbUrl": "http://images.google.com/images?q\u003dtbn:c6093fGTdNvKOM:stuff.fuzzymonkeyphotography.com/front_page/NEW_NEW_logo3_480px.jpg" 
    }, 
    { 
    "GsearchResultClass": "GimageSearch", 
    "width": "500", 
    "height": "375", 
    "imageId": "oKdBN2qxw5JJoM", 
    "tbWidth": "130", 
    "tbHeight": "98", 
    "unescapedUrl": "http://farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg?v\u003d0", 
    "url": "http://farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg%3Fv%3D0", 
    "visibleUrl": "www.flickr.com", 
    "title": "http://farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg?v\u003d0", 
    "titleNoFormatting": "http://farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg?v\u003d0", 
    "originalContextUrl": "http://www.flickr.com/photos/maryelizajade/1434841504/in/set-72157602146748073/", 
    "content": "\u003cb\u003efuzzy monkey\u003c/b\u003e", 
    "contentNoFormatting": "fuzzy monkey", 
    "tbUrl": "http://images.google.com/images?q\u003dtbn:oKdBN2qxw5JJoM:farm2.static.flickr.com/1104/1434841504_edc671e65c.jpg%3Fv%3D0" 
    }, 
    { 
    "GsearchResultClass": "GimageSearch", 
    "width": "500", 
    "height": "375", 
    "imageId": "GNgM5anX5NZYPM", 
    "tbWidth": "130", 
    "tbHeight": "98", 
    "unescapedUrl": "http://farm1.static.flickr.com/38/91607831_009166aa41.jpg", 
    "url": "http://farm1.static.flickr.com/38/91607831_009166aa41.jpg", 
    "visibleUrl": "www.flickr.com", 
    "title": "\u003cb\u003eFuzzy monkey\u003c/b\u003e sad face on Flickr - Photo Sharing!", 
    "titleNoFormatting": "Fuzzy monkey sad face on Flickr - Photo Sharing!", 
    "originalContextUrl": "http://www.flickr.com/photos/ajagendorf25/91607831/", 
    "content": "\u003cb\u003eFuzzy monkey\u003c/b\u003e sad face", 
    "contentNoFormatting": "Fuzzy monkey sad face", 
    "tbUrl": "http://images.google.com/images?q\u003dtbn:GNgM5anX5NZYPM:farm1.static.flickr.com/38/91607831_009166aa41.jpg" 
    } 
], 
"cursor": { 
    "pages": [ 
    { 
    "start": "0", 
    "label": 1 
    }, 
    { 
    "start": "4", 
    "label": 2 
    }, 
    { 
    "start": "8", 
    "label": 3 
    }, 
    { 
    "start": "12", 
    "label": 4 
    }, 
    { 
    "start": "16", 
    "label": 5 
    }, 
    { 
    "start": "20", 
    "label": 6 
    }, 
    { 
    "start": "24", 
    "label": 7 
    }, 
    { 
    "start": "28", 
    "label": 8 
    } 
    ], 
    "estimatedResultCount": "578000", 
    "currentPageIndex": 0, 
    "moreResultsUrl": "http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dfuzzy+monkey" 
} 
} 
, "responseDetails": null, "responseStatus": 200} 

我得到這個從谷歌圖片搜索API

響應

這裏是它的鏈接

https://developers.google.com/image-search/v1/jsondevguide

但是,當我嘗試解析它,我總是失敗沒有結果。

這裏是我的代碼

JSONObject json = new JSONObject(builder.toString()); 

      final JSONArray geodata = json.getJSONArray("results"); 
      final int n = geodata.length(); 
      for (int i = 0; i < n; ++i) { 
       final JSONObject person = geodata.getJSONObject(i); 

      System.out.println(person.geString("width")); 
      } 

有誰能夠告訴我,其中我做wrong.Any幫助將非常感激。 謝謝:)

+1

有誰能夠告訴我什麼是錯誤? – ruhungry

+0

呃,你在分析完它之後想用這個JSON來做什麼? – fge

+1

添加異常跟蹤得到你正在問的問題 – TechSpellBound

回答

3
JSONObject json = new JSONObject(builder.toString()); 
//Add this line 
JSONObject responseData = json.getJSONObject("responseData"); 
     final JSONArray geodata = responseData.getJSONArray("results"); 
     final int n = geodata.length(); 
     for (int i = 0; i < n; ++i) { 
      final JSONObject person = geodata.getJSONObject(i); 

     System.out.println(person.geString("width")); 
     } 
+0

謝謝zapdroid,它的工作:) –

+0

不客氣:) @TashenJazbi – zapdroid

1
JSONParser parser=new JSONParser(); 
Object obj = parser.parse(builder.toString()); 
JSONArray array = (JSONArray)obj; 
System.out.println("The 2nd element of array"); 
System.out.println(array.get(1)); 
JSONObject obj2 = (JSONObject)array.get(1); 

試試這個方法。