简体   繁体   English

使用Retrofit解析包含具有不同属性的对象的JSON数组

[英]Parsing JSON array that contain objects with different attributes with Retrofit

how can I parse JSON array that contains JSON objects without names and each object have his own attributes in Android with Retrofit2. 如何使用Retrofit2解析包含不带名称的JSON对象的JSON数组,并且每个对象在Android中都具有自己的属性。 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: 我有这样的POJO:

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 这就是我的Retrofit客户端getData方法的方式

Call<List<MainResponse>> getData();

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 还有你的getData方法

Call<List<MainResponse>> getData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM