Fetching json object and array from retrofit

前端 未结 2 1025
执念已碎
执念已碎 2021-01-12 20:21

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

sample json is

{
  \"students\": [
    {
      \"id\": \"         


        
2条回答
  •  耶瑟儿~
    2021-01-12 20:30

    Retrofit will automatically parse JSON Object as well JSON Array.

    @GET("/register?email=example@123.com")
    public void responseString(Callback response);
    

    your model class looks like:

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

    Then in response:

    @Override
    public void onResponse(Response 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()
        }
    }
    

提交回复
热议问题