使用Java中的GSON解析复杂的Json对象 [英] Parsing a complex Json Object using GSON in Java
问题描述
{我有一个非常长的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:正在使用中,
managed:false,
name:bt_newd20,
支持:{
status:not_supported,
原因:[
此卷不适用于管理,因为它已连接到虚拟机。要使用PowerVC管理此卷,请选择要连接的卷管理的虚拟机,所连接的卷将自动包含管理。
]
},
storage_pool:KVM,
mapped_wwpns:[
2101001B32BD4280,
2100001B329D4280,
2101001B32BD637E,
2100001B329D637E
],
id:c7838c79-17ca-3cbc-98e5-3567fde902d8,
size:0
$ status $ b $ managed
$ name $ vdisk138
support
managed:true,
status :{
status:supported
},
storage_pool:Chassis2_IBMi,
id:b6d00783-9f8c-40b8-ad78-956b0299478c ,
size:100
}
]
}
从我和其他很少的地方,我发现我需要定义一个像下面这样的顶级容器,但我不知道如何完成它的定义
静态类VolumeContainer {
//我不知道这里有什么。这是第一个问题
}
然后为每个 Volume
static class Volume {
private String status;
私有布尔管理;
私人字符串名称;
//这是第二个问题。支持变量不应该是一个字符串。
//它在{}中。仅供参考,我不会使用它。
//私人字符串支持;
private String storagePool;
私人列表< String> mapped_wwpns;
私人字符串ID;
私人字符串大小;
$ b我想解析它,到目前为止: JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(response).getAsJsonObject();
Gson gson = new Gson();
JSON字符串存储在名为response的变量中
VolumeContainer vc = gson.fromJson(response,VolumeContainer.class);
我最后的要求是 HashTable
of id
和相关的 name
。第一个问题:您的 VolumeContainer
需要:
public class VolumeContainer {
public List< Volume>卷;
}
它不需要是静态的。
第二个问题:你的 Volume
类应该是这样的:
public class Volume {
private String status;
私有布尔管理;
私人字符串名称;
私人支持支持;
private String storage_pool;
私人字符串ID;
private int size;
私人列表< String> mapped_wwpns;
public String getId(){return id;}
public String getName(){return name;}
}
我定义了一个名为 Support
的类,如下所示:
public class Support {
private String status;
私人列表< String>原因;
第三个问题:解析,如果 response
string包含您的示例数据,只需解析如下:
Gson g = new GSON();
VolumeContainer vc = g.fromJson(response,VolumeContainer.class);
第四个问题:获取地图。最后得到 HashMap
,就这样做:
HashMap<字符串,字符串> hm = new HashMap< String,String>(); (卷v:vc.volumes)
{
hm.put(v.getId(),v.getName());
}
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
.
解决方案 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());
}
这篇关于使用Java中的GSON解析复杂的Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!