简体   繁体   English

翻新2:正确解析带有多个数组的JSON的方法

[英]android - Retrofit 2:Correct way of Parsing JSON with multiple arrays

I have started using retrofit:2.0.2 first time into my new project and I have done with first service call using retrofit but am not sure is it correct way or not here is what i have done 我第一次开始在新项目中使用Retrofit:2.0.2,并且我已经完成了使用Retrofit进行的首次服务呼叫,但不确定这是否正确,这就是我所做的

web services response 网络服务响应

{
  "status": true,
  "message": "",
  "data": {
    "schools": [ { "id": "1", "name": "test 1" }, { "id": "2", "name": "test 12" }],
    "referrals": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
    "Brands": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
    "Teams": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
    "positions": [ { "id": "195", "name": "test 1" }, { "id": "1483", "name": "test 12" }],
  }
}

created 3 model classes to map above response 创建了3个模型类以映射以上响应

 public class SimpleObject {
    int id;
    String name;

// getter setter
}

public class SimpleData {

    private List<SimpleObject> schools = new ArrayList<SimpleObject>();
    private List<SimpleObject> referrals = new ArrayList<SimpleObject>();
    private List<SimpleObject> positions = new ArrayList<SimpleObject>();
    private List<SimpleObject> Teams = new ArrayList<SimpleObject>();
    private List<SimpleObject> Brands = new ArrayList<SimpleObject>();

// getter, setter
}


public class ResponseData{

    boolean status;
    String message;
    SimpleData data;

    //    getter setter
}

and then made a service call using Retrofit2 然后使用Retrofit2进行服务呼叫

call.enqueue( new Callback<ResponseData>() {
  @Override
  public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {

  }

  @Override
  public void onFailure(Call<ResponseData> call, Throwable t) {

  }

and its working fine but a want to insure that is it best way of doing this or can any one suggest the best way of handling such a response without creating multiple model classes for simple data (there should me only one model class "SimpleObject" and other will be list of "SimpleObject") 及其工作正常,但要确保这是执行此操作的最佳方法,或者可以建议任何人为处理此类响应而无需为简单数据创建多个模型类的最佳方法(应该只有一个模型类“ SimpleObject”和其他将是“ SimpleObject”的列表)

Please comment or suggest best way to handling response thanks. 请发表评论或提出处理回复的最佳方法。

The way you currently have it structured looks correct. 您目前使用的结构看起来很正确。 You need to have one model for every nested level in the JSON response, which you have. 您需要为JSON响应中的每个嵌套级别都有一个模型。

The only other option is simply breaking the object up using keys to pull all of the JSONObjects and JSONArrays, which I do not recommend. 唯一的其他选择是简单地使用键将对象分解,以拉出所有JSONObjects和JSONArrays,我不建议这样做。

copy and paste your json response in the link given below and download zip file and put it in your project directly. 将您的json响应复制并粘贴到下面给出的链接中,然后下载zip文件并将其直接放入您的项目中。

This link will convert your any kind of json response and convert it into POJO 此链接将转换您的任何json响应并将其转换为POJO

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

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