Gson把json串转换成java实体对象

Gson把json串转换成java实体对象的方法如下:

1.首先导入Gson的jar包,网上可以下载。

java实体对象如下:

public class Model {
private double Average;
private double Maximum;
private double Minimum;
private String instanceId;
private long timestamp;
private String userId; public double getAverage() {
return Average;
} public void setAverage(double average) {
Average = average;
} public double getMaximum() {
return Maximum;
} public void setMaximum(double maximum) {
Maximum = maximum;
} public double getMinimum() {
return Minimum;
} public void setMinimum(double minimum) {
Minimum = minimum;
} public String getInstanceId() {
return instanceId;
} public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
} public long getTimestamp() {
return timestamp;
} public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
} public String getUserId() {
return userId;
} public void setUserId(String userId) {
this.userId = userId;
}
}

转换代码如下所示:

String str = "{\"Average\":0.29,\"Maximum\":0.32,\"Minimum\":0.25,\"instanceId\":\"i-426auf2hf\",\"timestamp\":1449939900000,\"userId\":\"1467232540347717\"}";
Model model = new Gson().fromJson(str, Model.class);
System.out.println(model.getAverage());
上一篇:HDU 1716 排列2 (格式问题+排列)


下一篇:Ajax 原生和jQuery的ajax用法