java Gson包的一个不足之处

泛型:

public class ResponseEntity<T> {

public boolean isSuccess() {
return Success;
}

public void setSuccess(boolean success) {
Success = success;
}

public String getErrorMsg() {
return ErrorMsg;
}

public void setErrorMsg(String errorMsg) {
ErrorMsg = errorMsg;
}

public T getResult() {
return Result;
}

public void setResult(T result) {
Result = result;
}

public String getErrorNo() {
return ErrorNo;
}

public void setErrorNo(String errorNo) {
ErrorNo = errorNo;
}

private boolean Success;
private String ErrorMsg;
private T Result;
private String ErrorNo;
}


家庭成员类:

public class FamilyMember {

private long FmId;

private long FlId;

private String FmUser;

private String FmMobileNumber;

public long getFmId() {
return FmId;
}

public void setFmId(long fmId) {
FmId = fmId;
}

public long getFlId() {
return FlId;
}

public void setFlId(long flId) {
FlId = flId;
}

public String getFmUser() {
return FmUser;
}

public void setFmUser(String fmUser) {
FmUser = fmUser;
}

public String getFmMobileNumber() {
return FmMobileNumber;
}

public void setFmMobileNumber(String fmMobileNumber) {
FmMobileNumber = fmMobileNumber;
}

}

 

执行方法:

void Test()
{
ResponseEntity<FamilyMember> entity=new ResponseEntity<FamilyMember>();
entity.setErrorMsg("msg");
entity.setErrorNo("no");
entity.setSuccess(true);
FamilyMember member=new FamilyMember();
member.setFlId(1);
member.setFmId(1);
member.setFmMobileNumber("12345");
member.setFmUser("xiao");
entity.setResult(member);
Gson gson2=new Gson();
String json = gson2.toJson(entity);
ResponseEntity<FamilyMember> entity2= gson2.fromJson(json, ResponseEntity.class);
FamilyMember member2 = entity2.getResult();
String mobile = member2.getFmMobileNumber();

}

 

当Gson自己生成的json内容,都不允许被反解析。

更可直接说明的是:Gson不完全支持泛型。

你可能感兴趣的:(java)