2013-10-23 16 views
23

ho una lunghissima JSON per analizzare con GSON, ma per brevità ho rifilato a questo esempio:analisi di un complesso oggetto JSON utilizzando GSON in Java

{ 
"volumes": [ 
    { 
    "status": "available", 
    "managed": true, 
    "name": "va_85621143-1133-412f-83b4-57a01a552638_", 
    "support": { 
    "status": "supported" 
    }, 
    "storage_pool": "pfm9253_pfm9254_new", 
    "id": "afb8e294-6188-4907-9f6f-963c7623cecb", 
    "size": 9 
    }, 
    { 
    "status": "in-use", 
    "managed": false, 
    "name": "bt_newd20", 
    "support": { 
    "status": "not_supported", 
    "reasons": [ 
    "This volume is not a candidate for management because it is already attached to a virtual machine. To manage this volume with PowerVC, select the virtual machine to which the volume is attached for management. The attached volume will be automatically included for management." 
    ] 
    }, 
    "storage_pool": "KVM", 
    "mapped_wwpns": [ 
    "2101001B32BD4280", 
    "2100001B329D4280", 
    "2101001B32BD637E", 
    "2100001B329D637E" 
    ], 
    "id": "c7838c79-17ca-3cbc-98e5-3567fde902d8", 
    "size": 0 
    }, 
    { 
    "status": "available", 
    "managed": true, 
    "name": "vdisk138", 
    "support": { 
    "status": "supported" 
    }, 
    "storage_pool": "Chassis2_IBMi", 
    "id": "b6d00783-9f8c-40b8-ad78-956b0299478c", 
    "size": 100 


    } 
] 
} 

da così e pochi altri luoghi, ho scoperto che ho bisogno di definire un contenitore di livello superiore, come quella qui sotto, ma non so come completare la sua definizione

static class VolumeContainer {   
//I don't know what do in here. This is the first problem 
} 

e poi una classe per ogni Volume

static class Volume { 
    private String status; 
    private boolean managed; 
    private String name; 

    //This is the second problem.The "support" variable should not be a string. 
    //It is in {}. Just for information, I won't use it. 
    //private String support; 

    private String storagePool; 
    private List<String> mapped_wwpns; 
    private String id; 
    private String size; 

} 

Sto cercando di analizzarlo e questo è ciò che ho codificato finora:

JsonParser parser = new JsonParser(); 
JsonObject obj = parser.parse(response).getAsJsonObject(); 

Gson gson = new Gson(); 

La stringa JSON è memorizzato in una variabile denominata risposta

VolumeContainer vc = gson.fromJson(response,VolumeContainer.class); 

mia esigenza finale è un HashTable di id e associati name.

risposta

43

Primo problema: le vostre VolumeContainer deve essere:

public class VolumeContainer { 
    public List<Volume> volumes; 
} 

che non ha bisogno di essere statico.

Secondo problema: la classe Volume dovrebbe essere simile a questo:

public class Volume { 
    private String status; 
    private Boolean managed; 
    private String name; 
    private Support support; 
    private String storage_pool; 
    private String id; 
    private int size; 
    private List<String> mapped_wwpns; 

    public String getId(){return id;} 
    public String getName(){return name;} 
} 

ho definito una classe denominata Support come questo:

public class Support { 
    private String status; 
    private List<String> reasons; 
} 

Terzo problema: analisi, se response stringa contiene i dati di esempio, semplicemente analizza in questo modo:

Gson g = new Gson(); 
VolumeContainer vc = g.fromJson(response, VolumeContainer.class); 

Quarto problema: ottenere la mappa. Infine per ottenere il vostro HashMap, basta fare in questo modo:

HashMap<String, String> hm = new HashMap<String,String>(); 
for(Volume v: vc.volumes){ 
    hm.put(v.getId(), v.getName()); 
} 
+0

Grazie mille, che ha funzionato come un fascino. Nell'esempio Gson hanno fornito che hanno reso la classe statica, puoi spiegare perché l'hanno fatto e perché non dovrei? http://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java –

+0

You ' benvenuto. Hanno fatto una classe statica poiché era una classe interna (hanno mantenuto l'esempio compatto e poiché Gson non funziona bene con classi interne non statiche). Se dichiari le tue classi in file sorgente separati, non hai affatto bisogno di statico. Vedi questo: http://stackoverflow.com/questions/19449761/gson-does-not-deserialize-reference-to-outer-class/19459605#19459605 – giampaolo

+0

I "volumi" non sono pubblici? Vedendo come stai accedendo come è qui 'vc.volumes' (Nel quarto problema). O mi sta sfuggendo qualcosa? – jackdh

Problemi correlati