2013-07-26 6 views
-1

Je suit exemple de réponse de lecture d'URL, besoin de lire « objets » les analyser et convertir à une listeréponse Parse JSON avec méta et objets à l'aide Gson

exemple de réponse du serveur:

{ 
    "meta": { 
     "limit": 20, 
     "next": null, 
     "offset": 0, 
     "previous": null, 
     "total_count": 2 
    }, 
    "objects": [ 
     { 
      "body": "Body of my review", 
      "created_by": { 
       "first_name": "Josh", 
       "id": 2, 
       "last_name": "Bothun", 
       "username": "josh.bothun.1" 
      }, 
      "id": 1, 
      "location": 1, 
      "rating": 2, 
      "title": "Title of my review" 
     }, 
     { 
      "body": "This place is fantastic!", 
      "created_by": { 
       "first_name": "Josh", 
       "id": 1, 
       "last_name": "Bothun", 
       "username": "josh.bothun.1" 
      }, 
      "id": 2, 
      "location": 1, 
      "rating": 5, 
      "title": "Another review" 
     } 
    ] 
} 

Je le code suivant, mais incapable de récupérer la liste des objets

{   
    Gson GsonObject = new Gson(); 

String Test = "{\"meta\": {\"limit\": 20,\"next\": null,\"offset\": 0,\"previous\": null,\"total_count\": 2},\"objects\": [ { \"body\": \"Body of my review\",\"created_by\": { \"first_name\": \"Josh\", \"id\": 2, \"last_name\": \"Bothun\",\"username\": \"josh.bothun.1\"}, \"id\": 1,\"location\": 1, \"rating\": 2, \"title\": \"Title of my review\" }, { \"body\": \"This place is fantastic!\", \"created_by\": { \"first_name\": \"Josh\", \"id\": 1, \"last_name\": \"Bothun\", \"username\": \"josh.bothun.1\" }, \"id\": 2,\"location\": 1, \"rating\": 5, \"title\": \"Another review\" } ]}"; 

Type collectionType = new TypeToken<Collection<TabClass>>(){}.getType(); 
List<TabClass> enums = GsonObject.fromJson(Test, collectionType); 

System.out.println(GsonObject.toJson(enums));    

} 

class TabClass 
{ 
    private List meta; 
    private List objects; 
} 
+0

Votre fichier JSON n'est pas une collection d'objets 'TabClass'. –

Répondre

1

Utilisez l'outil jsonschema2pojo pour avoir le modèle approprié pour votre schéma JSON