2014-07-18 207 views
0

我試圖解析這個JSON使用gson,但我不能。 任何人都可以幫助我解析這個JSON使用gson使用Gson嵌套的Json解析

JSON:

{ 
    "status": "ok", 
    "results": { 
     "favourites": [ 
      { 
       "id": "UB3172", 
       "name": "Masdar Headquarters" 
      }, 
      { 
       "id": "UB1438", 
       "name": "Guggenheim Abu Dhabi on Saadiyat Island" 
      }, 
      { 
       "id": "UB4838", 
       "name": "Watani Residential Development in Abu Dhabi - 600 Villas and 48 Buildings" 
      }, 
      { 
       "id": "UB4795", 
       "name": "Two Mosques in Mohammed Bin Zayed City" 
      }, 
      { 
       "id": "UB1274", 
       "name": "2 Workers Residential City at Al Ain Industrial City" 
      } 
     ] 
    } 
} 

我想這一個JSON解析器類:

public class ProjectList { 
    public String status; 
    public String results; 

    public class Favourites{ 
    public String id; 
    public String name; 
    } 

} 

在MainActivit

Reader reader = new InputStreamReader(result); 
     Gson gson=new Gson(); 
     List<ProjectList.Favourites> fav=new ArrayList<ProjectList.Favourites>(); 
     fav=Arrays.asList(gson.fromJson(reader, ProjectList.Favourites.class)); 
+0

你有沒有嘗試過。 –

+0

是的..我已經試過 – Ansar

+0

你可以發佈你的代碼。 –

回答

2

創建一個POJO類如下

class MyResponse { 
     public String status; 

     public Result results; 

     public static class Favourites { 

      public String id; 
      public String name; 
     } 

     public static class Result { 

      public List<Favourites> favourites; 
     } 

    } 

,並把它傳遞給GSON作爲

MyResponse response = new Gson().fromJson(yourResponse, MyResponse.class); 

想法是保持鍵值對的層次結構與相應的POJO的

+0

我認爲類應該是公共的,不要忘記編輯proguard obfucator(如果涉及)... :) – Trinimon

+0

我是如何檢索這些id和名稱使用這個類。 – Ansar

+0

使內部類公共靜態看到編輯答案 – NullPointerException

1

你可以在這裏生成你的POJO:http://www.jsonschema2pojo.org/

有時gson不能從json轉換你的對象。在這種情況下,您必須編寫自己的解串器並將其與gson builder一起使用。編輯:如果您在發佈您的項目之前使用了proguard(如果您設置了proguard來更改您的pojos變量名稱),則gson不能匹配類變量名稱,json名稱,因此它無法轉換您的對象。您必須添加@SerializedName(「your_variable_name」)註釋。

+0

+1重要的東西 – NullPointerException

0

試試這種方式,希望這會幫助你解決你的問題。

String jsonRespone = "{\"status\":\"ok\",\"results\":{\"favourites\":[{\"id\":\"UB3172\",\"name\":\"Masdar Headquarters\"},{\"id\":\"UB1438\",\"name\":\"Guggenheim Abu Dhabi on Saadiyat Island\"},{\"id\":\"UB4838\",\"name\":\"Watani Residential Development in Abu Dhabi - 600 Villas and 48 Buildings\"},{\"id\":\"UB4795\",\"name\":\"Two Mosques in Mohammed Bin Zayed City\"},{\"id\":\"UB1274\",\"name\":\"2 Workers Residential City at Al Ain Industrial City\"}]}}"; 

String status; 
ArrayList<HashMap<String,String>> favouritesList = new ArrayList<HashMap<String, String>>(); 

try{ 
    JSONObject responseJson = new JSONObject(jsonRespone); 
    status = responseJson.getString("status"); 

    JSONArray favouriteJsonArray = responseJson.getJSONObject("results").getJSONArray("favourites"); 

    for (int i=0;i<favouriteJsonArray.length();i++){ 
     HashMap<String,String> favourite = new HashMap<String, String>(); 
     favourite.put("id",favouriteJsonArray.getJSONObject(i).getString("id")); 
     favourite.put("name",favouriteJsonArray.getJSONObject(i).getString("name")); 
     favouritesList.add(favourite); 
    } 

    System.out.print("status : "+status); 
    for (HashMap<String, String> favourite : favouritesList) { 
     System.out.print("id : "+favourite.get("id")); 
     System.out.print("name : "+favourite.get("name")); 
    } 
}catch (Throwable e){ 
    e.printStackTrace(); 
}