-2

Hi I already used GSON for parsing simple jsonobjects. But this time my response data is much complex. So I am struggling to parse that data.My data looks like :

[{"creator":"1", "users":[{"userName":"nilesh", "userAge":"25"},{"userName":"Me", "userAge":"25"}]},
{"creator":"2", "users":[{"userName":"nilesh", "userAge":"25"},{"userName":"Me", "userAge":"25"}]}
]

So I wrote one parsing class in following ways

public class UserData 
{
    @SerializedName("creator")
    public String creator;

    public List<Users> users;

    public static class Users
    {
        @SerializedName("userName")
        public String userName;

        @SerializedName("userAge")
        public String userAge;
    }
}

But this is not working. Am I doing some thing wrong need some help regarding this parsing. thank you.

I tried to parse like this :

Gson gson = new Gson();
UserData users = gson.fromJson(result, UserData.class);

And it gives me error like this :

 01-04 12:36:04.337: E/AndroidRuntime(15651): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • What is the error you are getting? Logcat? Your code? – johntheripp3r Jan 04 '14 at 06:59
  • See my answer. which is posted below – johntheripp3r Jan 04 '14 at 07:08
  • @nilkash - Make friends with [JSONLint.com](http://jsonlint.com/). It would have shown you the error immediately. Try it! – FoggyDay Jan 04 '14 at 07:11
  • @FoggyDay Thank you for replay. I checked on your given link and it shows me that is valid json. My problem is regarding parsing class. I am feeling difficulty in writing class for parsing my response. So if you have any idea regarding this please replay. – nilkash Jan 04 '14 at 07:17
  • see http://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array' – Shayan Pourvatan Jan 04 '14 at 07:29

2 Answers2

-1

It is because your JSON you posted is invalid.
It should be like this The posted json below is your json which is valid.

[
{
    "creator": "1",
    "users": [
        {
            "userName": "nilesh",
            "userAge": "25"
        },
        {
            "userName": "Me",
            "userAge": "25"
        }
    ]
},
{
    "creator": "2",
    "users": [
        {
            "userName": "nilesh",
            "userAge": "25"
        },
        {
            "userName": "Me",
            "userAge": "25"
        }
    ]
}
]

The json you have posted contains errors at following points

  1. users [ should be "users" :[
  2. userAge : should be "userAge":
johntheripp3r
  • 989
  • 6
  • 15
  • this is not my real json response. My json looks like this.I update my question data please check it. – nilkash Jan 04 '14 at 07:14
-1

This is what I was looking for

public class UserData 
{
    public String creator;

    public List<Users> user;

    public class Users
    {
        public String userName;
        public String userAge;
    }
}

and I parse in following manner

Gson gson = new Gson();
Type type = new TypeToken<List<UserData>>(){}.getType();
List<UserData > objList = gson.fromJson(result, type);
nilkash
  • 7,408
  • 32
  • 99
  • 176