-2

I receive the following String response from server (note the square brackets used in the values of users):

{
   "amount": 3,
   "users": [
      [
         "username1",
         "Name1"
      ],
      [
         "username2",
         "Name2"
      ],
      [
         "username3",
         "Name3"
      ]
   ]
}

How can I convert it to a List of User objects?? Preferably using GSON.

P.S. My User class contains fields username and name.

P.S.S. This post is NOT a duplicate for this post, since the formats of the JSON Outputs are completely different (in my case, the format is different from JSONArray fromat) and the answers to that post does not solve my problem...

Thank you!

Daniil Orekhov
  • 428
  • 1
  • 6
  • 20
  • Here is how I managed to solve this problem: [link](https://stackoverflow.com/questions/44188105/java-convert-string-representation-of-array-of-arrays-to-an-list-of-objects?noredirect=1#comment75390511_44188105) (which is not a duplicate btw..) – Daniil Orekhov May 25 '17 at 20:04

2 Answers2

2

check this

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(response.getJSONArray("users"), listType);
0

Use below link to convert your JSON to models

jsonschema

  • Choose Source type JSON and Annotation style Gson

Gson code:

Gson mGson = new Gson();
Type listType = new TypeToken<List<users>>() {}.getType();
List<users> object = mGson.fromJson(jsonString, listType);

hope this will help your doubt.

Jeeva
  • 1,166
  • 4
  • 18
  • 39