我想从[this link] [1]:
https://api.myjson.com/bins/38ln5使用改造来获取json
样本json是
- {
- "students": [
- {
- "id": "1","name": "Larzobispa"
- },{
- "id": "2","name": "La Cibeles"
- }
- ]
- }
请详细说明如何做到这一点.
非常感谢,伙计们!
解决方法
Retrofit将自动解析JSON Object以及JSON Array.
- @GET("/register?email=example@123.com")
- 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()
- }
- }