您可以使用Jackson 2 Library 将您的 JSON 字符串转换为 Java 对象。
在您的具体情况下,您可以例如创建以下类:
Disk.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"path", "space_used"})
public class Disk implements Serializable {
private final static long serialVersionUID = -6127352847480270783L;
@JsonProperty("path")
private String path;
@JsonProperty("space_used")
private Double spaceUsed;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("path")
public String getPath() {
return path;
}
@JsonProperty("path")
public void setPath(String path) {
this.path = path;
}
@JsonProperty("space_used")
public Double getSpaceUsed() {
return spaceUsed;
}
@JsonProperty("space_used")
public void setSpaceUsed(Double spaceUsed) {
this.spaceUsed = spaceUsed;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Host.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"disks", "hostname"})
public class Host implements Serializable {
private final static long serialVersionUID = -1972892789688333505L;
@JsonProperty("disks")
private List<Disk> disks = null;
@JsonProperty("hostname")
private String hostname;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("disks")
public List<Disk> getDisks() {
return disks;
}
@JsonProperty("disks")
public void setDisks(List<Disk> disks) {
this.disks = disks;
}
@JsonProperty("hostname")
public String getHostname() {
return hostname;
}
@JsonProperty("hostname")
public void setHostname(String hostname) {
this.hostname = hostname;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
HostContainer.java
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"hosts"})
public class HostContainer implements Serializable {
private final static long serialVersionUID = 7917934809738573749L;
@JsonProperty("hosts")
private List<Host> hosts = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("hosts")
public List<Host> getHosts() {
return hosts;
}
@JsonProperty("hosts")
public void setHosts(List<Host> hosts) {
this.hosts = hosts;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
用法:
public void parseMessage() throws IOException {
String msg = "{\"hosts\":[{\"disks\":[{\"path\":\"/\",\"space_used\":0.608}]," +
"\"hostname\":\"host1\"},{\"disks\":[{\"path\":\"/\",\"space_used\":0.79}," +
"{\"path\":\"/opt\",\"space_used\":0.999}],\"hostname\":\"host2\"}," +
"{\"disks\":[{\"path\":\"/\",\"space_used\":0.107}],\"hostname\":\"host3\"}]}";
ObjectMapper mapper = new ObjectMapper();
// This object will contain all the information you need, access it using e.g. getHosts();
HostContainer hostContainer = mapper.readValue(msg, HostContainer.class);
}