3

I have a json like this:

[{"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"}]

I have classes:

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;
}

}

and: `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 ...}`

I always get an excepiton:

10-08 15:17:46.600: W/System.err(2557): java.lang.ClassCastException: java.util.ArrayList and I do not understand why. here is my line to parse:

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

Please help on me, thx

3 Answers 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"
          } 
    ] 
}
Sign up to request clarification or add additional context in comments.

1 Comment

btw he can add them on runtime before start parsing.
0

in your json string;
value of "id" is defined as Integer and you are trying to parse it into string,
i think that's the root cause of the exception...

in Todo class, it is defined as;

@SerializedName("id")
private String id;

but in json string it is defined as;

"id":1

to fix that, either you can update field in your class to

@SerializedName("id")
private Integer id;

or in your json string to

"id":"1"

hope this helps.

Comments

0

First of all I would like to say thank you for you cooperate in this question, the soulution was about my parser, I mean I had changed the code for the following:

        JSONTokener jsonTokener = new JSONTokener(sb.toString());
        JSONArray rootArray = (JSONArray) jsonTokener.nextValue();
        rootArray.toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.