3

我有一个这样的json:

[{"campaignName":"2012 Q1","id":1,"source":"Added by subject","status":"Draft","subject":"Test Subject1"},{"campaignName":"2012 Q2","id":2,"source":"Added by reviewer","status":"Created","subject":"Test Subject2"},{"campaignName":"2012 Q2","id":3,"source":"Added by reviewer","status":"Created","subject":"Test Subject62"}]

我有课:

public class Json_ToDoData {
private ArrayList<ToDo> mList;

public Json_ToDoData() {

}

public ArrayList<ToDo> getmList() {
    return mList;
}

public void setmList(ArrayList<ToDo> mList) {
    this.mList = mList;
}

}

和: `public class ToDo { public ToDo() { }

@SerializedName("campaignName")
private String campaignName;

@SerializedName("id")
private String id;

@SerializedName("source")
private String source;

@SerializedName("status")
private String status;

@SerializedName("subject")
private String subject; ... gettes and settes ...}`

我总是得到一个例外:

10-08 15:17:46.600: W/System.err(2557): java.lang.ClassCastException: java.util.ArrayList 我不明白为什么。这是我要解析的行:

            Json_ToDoData rr = new JSONDeserializer<Json_ToDoData>().deserialize(sb.toString(), Json_ToDoData.class);

请帮帮我,谢谢

4

3 回答 3

1

I think the issue is that user json string is not in proper format. Try to format your json string in below format:

{ 
    jsonObjectName: 
    [ 
          { 
             "campaignName":"2012 Q1", 
             "id":1,
             "source":"Added by subject",
             "status":"Draft",
             "subject":"Test Subject1"
          } 
    ] 
}
于 2012-10-08T16:00:36.330 回答
0

首先我想说谢谢你在这个问题上的合作,解决方案是关于我的解析器,我的意思是我已经改变了以下代码:

        JSONTokener jsonTokener = new JSONTokener(sb.toString());
        JSONArray rootArray = (JSONArray) jsonTokener.nextValue();
        rootArray.toString();
于 2012-10-09T07:23:41.653 回答
0

在你的 json 字符串中;
“id”的值被定义为整数,你正试图将它解析成字符串,
我认为这是异常的根本原因......

在 Todo 类中,它被定义为;

@SerializedName("id")
private String id;

但在 json 字符串中,它被定义为;

"id":1

要解决这个问题,您可以将班级中的字段更新为

@SerializedName("id")
private Integer id;

或在您的 json 字符串中

"id":"1"

希望这可以帮助。

于 2012-10-08T16:07:39.457 回答