我有一个jsonstring,如下所示
{"coaList":[{"iD":324,"strName":"Cash","hasChildren":false,"amount":3500.0,"transDate":1421346600000},{"iD":326,"strName":"Cash","hasChildren":false,"amount":2000.0,"transDate":1421346600000},{"iD":328,"strName":"HDFC Bank","hasChildren":false,"amount":2500.0,"transDate":1421346600000}]}
我需要将此字符串转换为CoaAccountList类对象.Below是CoaAccountList类。 CoaAccountList.java:
public class CoaAccountList implements Serializable {
private List<COAAccount> coaList;
public List<COAAccount> getCoaList() {
return coaList;
}
public void setCoaList(List<COAAccount> coaList) {
this.coaList = coaList;
}
}
其中COAAccount类包含transDate作为TimeStamp变量,而在json字符串中它作为Long变量,所以我的转换过程为这个长transDate值产生com.google.gson.JsonSyntaxException。下面是我用来转换json字符串的代码到CoaAccountList
Gson gson = new Gson();
CoaAccountList transHisList=gson.fromJson(jsonString, CoaAccountList.class);
以下是COAAccount类。
COAAccount.java:
public class COAAccount implements Serializable {
private int iD;
private String strName;
private boolean hasChildren;
private float amount;
private Timestamp transDate;
public COAAccount() {
super();
// TODO Auto-generated constructor stub
}
public COAAccount(int iD, String strName, boolean hasChildren, float amount, Timestamp transDate) {
super();
this.iD = iD;
this.strName = strName;
this.hasChildren = hasChildren;
this.amount = amount;
this.transDate = transDate;
}
public int getiD() {
return iD;
}
public void setiD(int iD) {
this.iD = iD;
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public boolean isHasChildren() {
return hasChildren;
}
public void setHasChildren(boolean hasChildren) {
this.hasChildren = hasChildren;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public Timestamp getTransDate() {
return transDate;
}
public void setTransDate(Timestamp transDate) {
this.transDate = transDate;
}
@Override
public String toString() {
return strName;
}
}
请帮我将这个json字符串转换为CoaAccountList对象。
答案 0 :(得分:2)
在需要使用自定义反序列化模式的情况下,Gson允许您根据类型注册自己的反序列化代码。
为此,您首先需要在GsonBuilder中实例化Gson,而不是在以下代码中:
GsonBuilder gson = new GsonBuilder();
然后,在处理类型JsonDeserializer
时,您需要将GsonBuilder
个实例附加到Timestamp
,如下所示:
gson.registerTypeAdapter(Timestamp.class, new GsonTimestampDeserializer());
注意:GsonTimestampDeserializer
是我们在下面创建的自定义类,您可以在技术上将其命名为任意内容。
注册自定义反序列化器时,当需要将原始类型(int,string,double等)转换为指定的注册类型时,Gson将根据需要自动从JsonDeserializer接口调用deserialize()方法。为了利用这一点,我们实现了如下的解串器:
private class GsonTimestampDeserializer implements JsonDeserializer<Timestamp>{
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException{
return new Timestamp(json.getAsJsonPrimitive().getAsLong());
}
}
N.B:我假设您正在使用java.sql中的Timestamp对象,它提供了一个需要很长时间的构造函数。
全部放在一起:
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Timestamp.class, new GsonTimestampDeserializer());
private class GsonTimestampDeserializer implements JsonDeserializer<Timestamp>{
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException{
return new Timestamp(json.getAsLong());
}
}
Gson gsonInstance = gson.create();
CoaAccountList transHisList = gsonInstance.fromJson(jsonString, CoaAccountList.class);
当Gson解析JSON字符串并尝试填写相应的值时,它会遇到transDate上的Timestamp
类型,而不是使用java拥有的默认Object反序列化方案,使用我们提供的自定义反序列化方案来转换长到Timestamp对象。
这种方法可以推广到各种复杂的容器类,它们可以将数据存储为单一的基本类型,等效的GsonSerializer可以用来实现相反的。
答案 1 :(得分:1)
您需要创建一个自定义反序列化方法来解析时间戳。
E.g。 :
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Timestamp.class, new JsonDeserializer<Timestamp>() {
@Override
public Timestamp deserialize(JsonElement json, Type type, JsonDeserializationContext deserializationContext) throws JsonParseException {
return new Timestamp(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
CoaAccountList transHisList = gson.fromJson(jsonString, CoaAccountList.class);
答案 2 :(得分:0)
您需要在构建器中为Data.class注册Json反序列化程序。原始答案here
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});