有一个泛型类:
public class ResponseEntity<T> {
private boolean Success;
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 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;
}
}
现在有一个json 字符串:
String json ="{\"Success\":true,\"ErrorMsg\":\"\",\"ErrorNo\":\"\",\"Result\":{\"FmMobileNumber\":\"15555215554\",\"FmId\":3,\"FlId\":5,\"FmUser\":\"15555215554\"}}";
通过Gson包进行反解析,无法编译通过。
Gson gson = new Gson();
ResponseEntity<FamilyMember> entity = gson.fromJson(json,ResponseEntity<FamilyMember>.class);
求指教。如果使用
ResponseEntity<FamilyMember> entity = gson.fromJson(json,ResponseEntity.class);
FamilyMember result = entity.getResult();
entity.getResult() 将会是Map类型。
求指教。如果获取 ResponseEntity<FamilyMember> 这样的值。
和你一样的问题哦,等达人指教。
你好,已经被解决,详情见百度。http://zhidao.baidu.com/question/485449386.html?quesup2
@潇湘吹雨: 还是不行哦,我都直接复制你的了,编译通过,运行有异常。
导入包:
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
try { String result ="{\"Success\":true,\"ErrorMsg\":\"\",\"ErrorNo\":\"\",\"Result\":{\"FmMobileNumber\":\"15555215554\",\"FmId\":3,\"FlId\":5,\"FmUser\":\"15555215554\"}}"; Gson gson = new Gson(); java.lang.reflect.Type type=new TypeToken<ResponseEntity<FamilyMember>>(){}.getType(); ResponseEntity<FamilyMember> entity = gson.fromJson(result,type); } catch(Exception e){ Log.e("log_tag", "Error parsing data "+e.toString()); }
运行到ResponseEntity<FamilyMember> entity = gson.fromJson(result,type);就出异常
直接奔溃,CATCH都没用