我这边请求到了一个微信返回的json字符串,在转换的时候出了一个奇怪的问题,断点调试之后,发现源码部分有一个奇怪的地方,传入token类被解析成了两个class类型,一个String,一个Token类型。我的Token类中是两个属性,一个String ,一个int。照我的理解来看应该是一个String类型,一个int类型才对。
第一次提问 ,描述可能很差,望见谅
public static void main(String[] args) {
String url = WxUrlConst.get_access_token_url.replace("APPID", WxVisitConst.APP_ID).replace("APPSECRET", WxVisitConst.APP_SECRET_KEY);
if(token == null){
String jsonstr = Http.get(url).getContent();
Json.fromJsonAsMap(Token.class, jsonstr); //这一行出错了
//请求微信后返回的参数 {"access_token":"iek3OXm9t0JFePnX0GxjQUdBJJqRlDrCIkvWshGwOa0FKu5AKbDs7tS34_cmTkRBTLUan7zatDfoKqxlrGiSk8dkWJWC //SbysE5PbE4SkdY39h0A_M0fnMgCRCtXcROmqSDWbAIAEPI","expires_in":7200}
}
}
异常:
Exception in thread "main" org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.String> to <com.sitronnet.weixin.bean.to.authorise.Token> for {jKin458dHNXtRjhBt6ME7NV66lwhD0Mg6ZCLMTNCbzjWkvNWKs-sjBen6YpqWjBNi8brr9rbxtgbNqIazJnLUaJXxPr-_lfx49QS22bH5wDrEqv1cXbnFKY3CHQcAPN2LREiAGAGMZ} because:
BorningException:Fail to born 'com.sitronnet.weixin.bean.to.authorise.Token'
by args: [
@(jKin458dHNXtRjhBt6ME7NV66lwhD0Mg6ZCLMTNCbzjWkvNWKs-sjBen6YpqWjBNi8brr9rbxtgbNqIazJnLUaJXxPr-_lfx49QS22bH5wDrEqv1cXbnFKY3CHQcAPN2LREiAGAGMZ)]
at org.nutz.castor.Castors.cast(Castors.java:264)
at org.nutz.castor.Castors.castTo(Castors.java:318)
at org.nutz.mapl.impl.convert.ObjConvertImpl.injectMap(ObjConvertImpl.java:133)
at org.nutz.mapl.impl.convert.ObjConvertImpl.inject(ObjConvertImpl.java:77)
at org.nutz.mapl.impl.convert.ObjConvertImpl.convert(ObjConvertImpl.java:65)
at org.nutz.mapl.Mapl.maplistToObj(Mapl.java:34)
at org.nutz.json.Json.parse(Json.java:77)
at org.nutz.json.Json.fromJson(Json.java:71)
at org.nutz.json.Json.fromJson(Json.java:93)
at org.nutz.json.Json.fromJsonAsMap(Json.java:392)
at com.sitronnet.weixin.common.GloBalObject.main(GloBalObject.java:57)
Caused by: org.nutz.lang.born.BorningException: Fail to born 'com.sitronnet.weixin.bean.to.authorise.Token'
by args: [
@(jKin458dHNXtRjhBt6ME7NV66lwhD0Mg6ZCLMTNCbzjWkvNWKs-sjBen6YpqWjBNi8brr9rbxtgbNqIazJnLUaJXxPr-_lfx49QS22bH5wDrEqv1cXbnFKY3CHQcAPN2LREiAGAGMZ)]
at org.nutz.lang.Mirror.born(Mirror.java:986)
at org.nutz.castor.castor.String2Object.cast(String2Object.java:16)
at org.nutz.castor.castor.String2Object.cast(String2Object.java:9)
at org.nutz.castor.Castors.cast(Castors.java:252)
... 10 more
我的Token类
public class Token {
//接口访问凭证
private String accessToken;
//凭证有效期,单位:秒
private int expiresIn;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public int getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}
}