【问题标题】:Parsing a complex Json Object using GSON in Java在 Java 中使用 GSON 解析复杂的 Json 对象
【发布时间】:2023-03-25 12:08:01
【问题描述】:

我有一个很长的 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


  }
 ]
}

从 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);

我的最终要求是HashTableid 和关联的name

【问题讨论】:

    标签: java json serialization gson


    【解决方案1】:

    第一个问题:您的VolumeContainer 必须是:

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

    它不需要是静态的。

    第二个问题:你的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;
    }
    

    第三个问题:解析,如果response字符串包含你的示例数据,只需像这样解析:

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

    第四题:获取地图。最后要得到你的HashMap,只需这样做:

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

    【讨论】:

    • 非常感谢,这就像一个魅力。在他们提供的 Gson 示例中,他们已将类设为静态,您能否解释一下他们为什么这样做以及为什么我不应该这样做? code.google.com/p/google-gson/source/browse/trunk/extras/src/…
    • 不客气。他们做了一个静态类,因为它是一个内部类(他们保持示例紧凑,因为 Gson 不适合非静态内部类)。如果您在单独的源文件中声明您的类,则根本不需要静态。看到这个:stackoverflow.com/questions/19449761/…
    • volumes 不应该公开吗?看到您正在访问它,就像在这里vc.volumes (在第四个问题中)。还是我错过了什么?
    • 两年后,我丢失了用于创建复查答案的原始代码。不管你是对的,它必须是公开的。要去编辑。谢谢。
    猜你喜欢
    • 2011-06-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多