本文介绍了从改造中获取json对象和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从[此链接] [1]中获取json: https://api.myjson.com/bins/38ln5 使用改造

I want to fetch json from [this link][1]: https://api.myjson.com/bins/38ln5 using retrofit

示例json是

{
  "students": [
    {
      "id": "1",
      "name": "Larzobispa"
    },
    {
      "id": "2",
      "name": "La Cibeles"
    }
  ]
}

请详细说明如何执行此操作.非常感谢,伙计们!

Please explain details how to do that.Thanks a lot, guys!

推荐答案

Retrofit会自动解析JSON对象以及JSON数组.

Retrofit will automatically parse JSON Object as well JSON Array.

@GET("/[email protected]")
public void responseString(Callback<Student> response);

您的模型类如下:

public class Student{
private ArrayList<StudentInfo> studentList = new ArrayList<>();
     //getter and setters
}

public class StudentInfo{
    private String id;
    private String name;
    //getters and setters
}

然后回应:

@Override
public void onResponse(Response<Student> response, Retrofit retrofit) {
    if (response.isSuccess()) {
        Student student = response.body;
        Log.e("Student name", student.getStudent().get(0).getName()); // do whatever you want
    }else{
        // get response.errorBody()
    }
}

这篇关于从改造中获取json对象和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:06