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":"[email protected]"
}
]

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();
1

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.