2015-08-15 59 views
0

我只是想知道,如果有人可以幫助我..使用GSON解析JSON(通過分形生成)

所以,我有我一直在使用分形來解析對象,生成一些漂亮的JSON開發的API被我正在製作的Android應用程序所使用。

的JSON輸出看起來有點像這樣:

{ 
    "data": [ 
     { 
      "id": 1, 
      "name": "John Smith", 
      "description": "Information about John Smith", 
      "games": { 
       "data": [ 
        { 
         "name": "Batman Arkham City", 
         "description": "Information about Game 1" 
        }, 
        { 
         "name": "Silent Hill", 
         "description": "Information about Game 2" 
        } 
       ] 
      } 
     } 
    ] 
} 

解析這個使用GSON,我明明要創建一個模型,它有一個ArrayList的的是..請注意數據關鍵?我如何指導Gson解析這個?我明白去除有JSON這個樣子的:

{ 
    "data": [ 
     { 
      "id": 1, 
      "name": "John Smith", 
      "description": "Information about John Smith", 
      "games": [ 
       { 
        "name": "Batman Arkham City", 
        "description": "Information about Game 1" 
       }, 
       { 
        "name": "Silent Hill", 
        "description": "Information about Game 2" 
       } 
      ] 
     } 
    ] 
} 

我一個人的模型看起來有點像這樣:

public class Person 
{ 
    public String name; 
    public ArrayList<Game> games; 

就像我說的,去掉數據鍵可以讓我將數據解析爲Java模型,如下所示:

peopleArray = jsonObject.getJSONArray("data"); 
Type listType = new TypeToken<ArrayList<Person>>() {}.getType(); 
ArrayList<Person> result = new Gson().fromJson(peopleArray.toString(), listType); 

所以基本上,有什麼辦法可以告訴Gson遊戲數組將有一個資料重點?對不起,信息超載,希望這會有道理?

Ta

回答