0

I'm trying to parse this array:

[{"style_image":"","count":5,"color":"red","ClusterName":"cluster1","CID":"[98,99,96,16,95]"},
{"style_image":"","count":1,"color":"red","ClusterName":"cluster2","CID":"[91]"}]

and I'm having with the "CID":"[98,99,96,16,95]" object. I get JsonParseException exceptions if my object looks like this:

@SerializedName("ClusterName")
private String name;
private String color;
@SerializedName("CID")
private List<Integer> levelOneIDs;
private int count;
private String style_image;

or this:

@SerializedName("ClusterName")
private String name;
private String color;
@SerializedName("CID")
private int[] levelOneIDs;
private int count;
private String style_image;

How do I parse the object with the array of integers in the object? It seems something easy enough, but I'm not sure how to do it.

1
  • Why not use the JSON library included in the Android SDK? Commented Jul 24, 2014 at 1:33

1 Answer 1

1

The problem is that "[98,99,96,16,95]" is not a JSON array- it is a string (notice the quotes around the array).

This will parse correctly:

@SerializedName("CID")
private String levelOneIDs;

However, this will obviously not get you the list of ints that you are looking for. You can either let GSON parse the rest of the JSON and handle parsing that field yourself, or you could wait for GSON to parse it into a string then populate a second field with the integers yourself.

If you have any sort of input on how the API you are using generates this JSON, you should update it to output a proper JSON array.

Sign up to request clarification or add additional context in comments.

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.