使用Java中的GSON解析复杂的Json对象

The*_*tar 25 java serialization json gson

我有一个非常长的JSON来解析Gson,但为了简洁起见,我将它修剪为这个例子:

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


  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

从SO和其他几个地方,我发现我需要定义一个顶层容器,如下面的一个,但我不知道如何完成它的定义

static class VolumeContainer {        
 //I don't know what do in here. This is the first problem 
}
Run Code Online (Sandbox Code Playgroud)

然后是每个人的一个班级 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;

}
Run Code Online (Sandbox Code Playgroud)

我试图解析它,这是我编码到目前为止:

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

Gson gson = new Gson();
Run Code Online (Sandbox Code Playgroud)

JSON字符串存储在名为response的变量中

VolumeContainer vc = gson.fromJson(response,VolumeContainer.class);
Run Code Online (Sandbox Code Playgroud)

我的最后一个要求是一个HashTableid和相关的name.

gia*_*olo 47

第一个问题:您VolumeContainer需要:

public class VolumeContainer {
   public List<Volume> volumes;
}
Run Code Online (Sandbox Code Playgroud)

它不需要是静态的.

第二个问题:你的Volume课应该是这样的:

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;}
}
Run Code Online (Sandbox Code Playgroud)

我定义了一个这样命名的Support类:

public class Support {
   private String status;
   private List<String> reasons;
}
Run Code Online (Sandbox Code Playgroud)

第三个问题:解析,如果responsestring包含你的示例数据,只需解析如下:

Gson g = new Gson();
VolumeContainer vc = g.fromJson(response, VolumeContainer.class);
Run Code Online (Sandbox Code Playgroud)

第四个问题:拿到地图.最后得到你的HashMap,就这样做:

HashMap<String, String> hm = new HashMap<String,String>();
for(Volume v: vc.volumes){
  hm.put(v.getId(), v.getName());  
}
Run Code Online (Sandbox Code Playgroud)