26

I have a very long JSON to parse with Gson, but for brevity I have trimmed it to this example:

{
 "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


  }
 ]
}

From SO and few other places, I have found that I need to define a top level container like one below but I do not know how to complete its definition

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

and then a class for each 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;

}

I am trying to parse it and this is what I coded so far:

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

Gson gson = new Gson();

The JSON string is stored in a variable named response

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

My final requirement is a HashTable of id and associated name.

giampaolo
  • 6,906
  • 5
  • 45
  • 73
The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35

1 Answers1

49

First problem: your VolumeContainer needs to be:

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

it does not need to be static.

Second problem: your Volume class should be like this:

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;}
}

I defined a class named Support like this:

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

Third problem: parsing, If response string contains your example data, simply parse like this:

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

Fourth problem: get the map. Finally to get your HashMap, just do like this:

HashMap<String, String> hm = new HashMap<String,String>();
for(Volume v: vc.volumes){
  hm.put(v.getId(), v.getName());  
}
giampaolo
  • 6,906
  • 5
  • 45
  • 73
  • Thanx a lot, that worked like a charm. In the Gson example they have provided they have made the class static, can you please explain why they did it and why I should not ?? http://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java – The_Lost_Avatar Oct 24 '13 at 05:46
  • You're welcome. They did a static class since it was a inner class (they kept example compact and since Gson does not do well with non-static inner classes). If you declare your classes in separate source files, you do not need static at all. See this: http://stackoverflow.com/questions/19449761/gson-does-not-deserialize-reference-to-outer-class/19459605#19459605 – giampaolo Oct 24 '13 at 05:56
  • Should `volumes` not be public? Seeing as you are accessing it like it is here `vc.volumes` (In fourth problem). Or am I missing something? – Jack Dec 07 '15 at 14:06
  • After two years I've lost original code I used to create the answer to double check. However you are right, it have to be public. Going to edit. Thanks. – giampaolo Dec 07 '15 at 17:08