2016-02-26 60 views
0

當我搜索任何標籤時,我正在從instagram api請求數據。作爲回報,我得到了大量的json數據,對應於20張圖片。下面的反應是我以前在網上生成我的POJO塊如何有效地獲取特定的json響應位?

{ 
    "pagination": { 
    "next_max_tag_id": "1193052000552992097", 
    "deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead", 
    "next_max_id": "1193052000552992097", 
    "next_min_id": "1193052554319844057", 
    "min_tag_id": "1193052554319844057", 
    "next_url": "https://api.instagram.com/v1/tags/cats/media/recent?access_token=631477962.1fb234f.f7c5cda97c7f4df983b1c764f066ed37&max_tag_id=1193052000552992097" 
    }, 
    "meta": { 
    "code": 200 
    }, 
    "data": [ 
    { 
     "attribution": null, 
     "tags": [ 
     "cats", 
     "caseworker", 
     "homestuck" 
     ], 
     "type": "image", 
     "location": null, 
     "comments": { 
     "count": 0, 
     "data": [] 
     }, 
     "filter": "Normal", 
     "created_time": "1456442969", 
     "link": "https://www.instagram.com/p/BCOkvoim1LZ/", 
     "likes": { 
     "count": 0, 
     "data": [] 
     }, 
     "images": { 
     "low_resolution": { 
      "url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/12729405_224148847934280_1450226662_n.jpg?ig_cache_key=MTE5MzA1MjU1NDMxOTg0NDA1Nw%3D%3D.2", 
      "width": 320, 
      "height": 320 
     }, 
     "thumbnail": { 
      "url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/12729405_224148847934280_1450226662_n.jpg?ig_cache_key=MTE5MzA1MjU1NDMxOTg0NDA1Nw%3D%3D.2", 
      "width": 150, 
      "height": 150 
     }, 
     "standard_resolution": { 
      "url": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/12729405_224148847934280_1450226662_n.jpg?ig_cache_key=MTE5MzA1MjU1NDMxOTg0NDA1Nw%3D%3D.2", 
      "width": 640, 
      "height": 640 
     } 
     }, 
     "users_in_photo": [], 
     "caption": { 
     "created_time": "1456442969", 
     "text": "Bitch! I'm fabulous! That's my case worker..she is obsessed with cats\n\n#cats #caseworker #homestuck", 
     "from": { 
      "username": "strider_inc", 
      "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12558836_953196128050469_1739102_a.jpg", 
      "id": "2322171747", 
      "full_name": "WE All 4EVER KAWAII TRASH GODS" 
     }, 
     "id": "1193052563471815092" 
     }, 
     "user_has_liked": false, 
     "id": "1193052554319844057_2322171747", 
     "user": { 
     "username": "strider_inc", 
     "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12558836_953196128050469_1739102_a.jpg", 
     "id": "2322171747", 
     "full_name": "WE All 4EVER KAWAII TRASH GODS" 
     } 
    } 

所以,當我這樣做,我得到像10-12不同的POJO類到了我應該映射此數據。現在第一...我只是試圖說出來,我100%生病有一些問題映射他們我的意思是吉森會爲我做的,但我不知道是否有任何更多的,我會需要等等

但最重要的是我的應用程序只需要低標準的url圖片,其他所有信息對我來說都沒用。

Ofcourse,我知道一種方法來做到這一點,它是把整個事情轉換成一個字符串,並通過多次解析整個字符串尋找關鍵詞等等,並製作圖像等我不想這樣做,因爲它的醜陋。它的工作原理,但我想要一個簡潔的方式做到這一點,同時沒有完全映射。

+0

由於您使用Gson基本上有兩個選擇:1.不映射到POJO,只需抓住JSON(Gson有JsonElement); 2.使用流API(Gson也有)。對不起,我通常使用傑克遜,所以我不知道上面兩種情況的代碼,但它應該很容易找到:) – fge

回答

0

使用Gson的JsonParser類,您可以將JSON解析爲JsonElements樹,然後只提取所需的數據。

例如,爲了打印出所有的低分辨率的網址,你可以使用下面的代碼:使用

String json = "..."; 

JsonParser parser = new JsonParser(); 
JsonObject object = parser.parse(json).getAsJsonObject(); 
JsonArray data = object.getAsJsonArray("data"); 

for (JsonElement element : data) { 
    JsonObject images = element.getAsJsonObject().getAsJsonObject("images"); 
    JsonObject lowResolution = images.getAsJsonObject("low_resolution"); 
    String url = lowResolution.getAsJsonPrimitive("url").getAsString(); 
    System.out.println(url); 
} 

你的榜樣JSON,這會打印:

https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/12729405_224148847934280_1450226662_n.jpg?ig_cache_key=MTE5MzA1MjU1NDMxOTg0NDA1Nw%3D%3D.2 
相關問題