26

Gson で解析する非常に長い JSON がありますが、簡潔にするために次の例に切り詰めました。

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


  }
 ]
}

SO および他のいくつかの場所から、以下のようなトップレベルのコンテナーを定義する必要があることがわかりましたが、その定義を完了する方法がわかりません

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

そして、それぞれのクラス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;

}

私はそれを解析しようとしていますが、これは私がこれまでにコーディングしたものです:

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

Gson gson = new Gson();

JSON 文字列は、response という名前の変数に格納されます

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

私の最後の要件は、HashTableofidと associatednameです。

4

1 に答える 1

49

最初の問題:あなたのVolumeContainer必要性:

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

静的である必要はありません。

2番目の問題: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;}
}

Support次のような名前のクラスを定義しました。

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

3 番目の問題: 解析、response文字列にサンプル データが含まれている場合は、次のように解析するだけです。

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

4 番目の問題: マップを取得します。最後に を取得するHashMapには、次のようにします。

HashMap<String, String> hm = new HashMap<String,String>();
for(Volume v: vc.volumes){
  hm.put(v.getId(), v.getName());  
}
于 2013-10-23T20:58:58.930 に答える