0

how can I parse JSON array that contains JSON objects without names and each object have his own attributes in Android with Retrofit2. Json is something like this:

[
{
    "username":"alexruskovski",
    "age":27,
    "active":true
},
{
    "languages":"Java",
    "occupation":"Programming",
    "phone_num":"123456789",
    "email":"asdf@qwe.com"
}
]

And I have my POJO's like this:

user:

   public class User{
      String username;
      int age;
      boolean active;
   }

and here is the data object:

public class Data{
   String languages,
   String occupation;
   String phone_num;
   String email;
}

and this is my main response class:

public class MainResponse{
   User user;
   Data data;
}   

And this is how my Retrofit client getData method is

Call<List<MainResponse>> getData();
Alexander
  • 333
  • 3
  • 17
  • have a [look here , using annotations and list of objects](https://stackoverflow.com/questions/42274551/how-to-parse-multiple-json-arrays-inside-a-json-object-using-gson) – Pavneet_Singh Nov 02 '17 at 13:27

1 Answers1

1

To parse that response you need the following class

  public class MainResponse{
    String username;
    int age;
    boolean active;
    String languages;
    String occupation;
    String phone_num;
    String email;
}

And your getData method

Call<List<MainResponse>> getData();
cherif
  • 1,164
  • 1
  • 11
  • 16
  • Its working, thanks. For anyone who will face similar problem in the feature, just to know the response list will contain all object from the JSON list. In example at index 0 the list will contain username, age and active. At index 1 will be language, occupation, phone_num and email. – Alexander Nov 02 '17 at 16:57