java gson fromjson返回非泛型类型的null

时间:2015-03-17 15:22:09

标签: java json gson

我之前已经讨论过这个话题,但我没有看到任何有我特色的人。标题描述了我的情况。两件事,我已经通过(我想想)这里所有可能的答案,这就是我写自己的问题的原因。第二件事,反序列化在另一个项目中完美无缺,并且我按原样复制(也许这就是问题所在)。我还检查了https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer但我无法找到我的代码有什么问题。所以这是我到目前为止: JSON:

{"offset":0,
"total":137,
"per-page":20,
"events":[
    {"id":286560,
    "name":"Arsenal vs Everton",
    "start":"2015-03-01T14:10:00.000Z",
    "status":"open",
    "sport-id":15,
    "category-id":[158208],
    "in-running-flag":true,
    "allow-live-betting":true,
    "market-ids":[1119280,1121697,1121698,1119271,1119299,1119587,1119288,1118302,1120869,1120870,1121696,1121900,1120868],
    "meta-tags":[
        {"id":402,
        "name":"Live Betting",
        "type":"COMPETITION"
        },
        {"id":19,
        "name":"England",
        "type":"COUNTRY"
        },
        {"id":32,
        "name":"Premier League",
        "type":"COMPETITION"
        },
        {"id":2285,
        "name":"March 1st 2015",
        "type":"DATE"
        },
        {"id":1,
        "name":"Sport",
        "type":"Root"
        },
        {"id":4,
        "name":"Soccer",
        "type":"SPORT"
        }]
    },
    {"id":286960,
    "name":"Norwich vs Ipswich Town",
    "start":"2015-03-01T14:10:00.000Z",
    "status":"open",
    "sport-id":15,
    "category-id":[158307],
    "in-running-flag":true,
    "allow-live-betting":true,
    "market-ids":[1121701,1121702,1122048,1120062,1120099,1121700,1120050,1120111,1120087,1121620],
    "meta-tags":[
        {"id":402,
        "name":"Live Betting",
        "type":"COMPETITION"
        },
        {"id":33,
        "name":"Championship",
        "type":"COMPETITION"
        },
        {"id":19,
        "name":"England",
        "type":"COUNTRY"
        },
        {"id":2285,
        "name":"March 1st 2015",
        "type":"DATE"
        },
        {"id":1,
        "name":"Sport",
        "type":"Root"
        },
        {"id":4,
        "name":"Soccer",
        "type":"SPORT"}]}
]}

班级:

public class Event {

private String id;
private String name;
private Date start;
private String status;
private String sportId;
private ArrayList<String> categoryId;
private boolean inRunningFlag;
private boolean allowLiveBetting;
private ArrayList<String> marketIds;
private List<MetaTags> metaTags;

public String getId(){
    return id;
}

public String getName(){
    return name;
}

public Date getStartDate(){
    return start;
}

public String getStatus(){
    return status;
}

public String getSportId(){
    return sportId;
}

public ArrayList<String> getCategoryId(){
    return categoryId;
}

public boolean getInRunningFlag(){
    return inRunningFlag;
}

public boolean getAllowLiveBetting(){
    return allowLiveBetting;
}

public ArrayList<String> getMarketIds(){
    return marketIds;
}

public List<MetaTags> getMetaTags(){
    return metaTags;
}

public void setId(String id){
    this.id = id;
}

public void setName(String name){
    this.name = name;
}

public void setStartDate(Date start){
    this.start = start;
}

public void setStatus(String status){
    this.status = status;
}

public void setSportId(String sportId){
    this.sportId = sportId;
}

public void setCategoryId(ArrayList<String> categoryId){
    this.categoryId = categoryId;
}

public void setInRunningFlag(boolean inRunningFlag){
    this.inRunningFlag = inRunningFlag;
}

public void setAllowLiveBetting(boolean allowLiveBetting){
    this.allowLiveBetting = allowLiveBetting;
}

public void setMarketIds(ArrayList<String> marketIds){
    this.marketIds = marketIds;
}

public void setMetaTags(List<MetaTags> metaTags){
    this.metaTags = metaTags;
}}

public class EventContainer extends Container{

private List<Event> result;

public List<Event> getResult(){
    return result;
}

public void setResult(List<Event> result){
    this.result = result;
}}

最后是从json转换为对象的过程

public List<Event> listEvents (String sessionToken) throws APIException{
    String result = getInstance().makeRequest(MatchBookApiCalls.LISTEVENTS.getApiCallName(), MatchBookApiCalls.LISTEVENTS.getURLSuffix(), sessionToken);

    EventContainer container = JsonConverter.convertFromJson(result, EventContainer.class);

    if(container.getError() != null)
        throw container.getError().getData().getAPIException();

    return container.getResult();
}

和最后一个:

public class JsonConverter {

/**
 * We needed to override the adapter for the Date class so to make all dates to be serialized in ISO8601 UTC
 * Just formatting the string to the ISO format does not adjust by the timezone on the Date instance during serialization.
 */
private static final Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new ISO8601DateTypeAdapter()).create();

/** This method deserializes the specified Json into an object of the specified class.
 *
 */
public static  <T> T convertFromJson(String toConvert,  Class<T>  clazz){
    return gson.fromJson(toConvert, clazz); // this is failing horribly
}

/** This method deserializes the specified Json into an object of the specified Type.
 *
 */
public static  <T> T convertFromJson(String toConvert,  Type  typeOfT){
    return gson.fromJson(toConvert, typeOfT);
}

/**
 * This method serializes the specified object into its equivalent Json representation.
 */
public static String convertToJson(Object toConvert){
    return gson.toJson(toConvert);

}}

我已经和它斗争了两天,所以也许我的大脑有点烧焦,答案是如此愚蠢我会把头撞在墙上,但我无法看到它。哦!错误是:

Exception in thread "main" java.lang.NullPointerException
at MatchBookApiJsonOperations.listEvents(MatchBookApiJsonOperations.java:46)
at MatchBookPlaceBet.getEvents(MatchBookPlaceBet.java:27)
at MatchBookConnection.testingEntities(MatchBookConnection.java:160)
at MatchBookMain.main(MatchBookMain.java:36)

提前感谢任何帮助

容器:

public class Container {

private Error error;
private String json;

public Error getError(){
return error;
}

public String getJson(){
return json;
}

public void setError(Error error){
this.error = error;
}

public void setJson(String json){
this.json = json;
}
}

1 个答案:

答案 0 :(得分:0)

好的,排序......我的好朋友J告诉我要将事件列表的名称更改为&#34; events&#34;而不是&#34;结果&#34;在EventContainer类中(我想匹配json对象&#34;事件&#34;),瞧!有用!!! tbh我不知道它必须与名称相匹配,我认为这是更通用或描述性的东西,但绝不会判断它是否正确解析它......有时你必须学习艰难的方法。为他人的利益干杯并愿意帮助!!