Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - Parse a nested json with retrofit 2.0

i have this json's and i want to use retrofit for parsing them.

{
  "status": "true",
  "data": [
{
  "id": "1",
  "title": "Hi :)",
  "text": "<p>121212</p>",
  "cat_id": "1",
  "username": "admin",
  "coin": "0",
  "datetime": "1451508880",
  "isShow": "1"
},
{
  "id": "3",
  "title": " Hi :)",
  "text": "Hi :)",
  "cat_id": "2",
  "username": "Hi :)",
  "coin": "20",
  "datetime": "1451508880",
  "isShow": "1"
},
{
  "id": "4",
  "title": "a",
  "text": "someText",
  "cat_id": "1",
  "username": "admin",
  "coin": "10",
  "datetime": "1451982292",
  "isShow": "1"
}
  ]
}

when that json is in array mode my code work. but my question is how parse nested json like above sample?

here my javaClass: (statusClass)

public class retroStatus {

@SerializedName("status")
private String status;

@SerializedName("data")
private ArrayList<retroPost> data;

  //getters And Setters
}

(retroPost Class)

public class retroPost {

@SerializedName("id")
private int id;

@SerializedName("title")
private String title;

@SerializedName("text")
private String text;

@SerializedName("cat_id")
private int cat_id;

@SerializedName("datetime")
private long datetime;


 //getters And Setters
}

(getPostInterface)

public interface getPost {
@GET("get/posts/all")
Call<List<retroStatus>> getPost();
}

(and mainCode)

  Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://mywebsite.it/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    getPost postService = retrofit.create(getPost.class);
    Call<List<retroStatus>> call = postService.getPost();
    Callback<List<retroStatus>> callback = new Callback<List<retroStatus>>() {
        @Override
        public void onResponse(Response<List<retroStatus>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                for (retroStatus status : response.body()) {
                    Log.e("test", "nowStatusIs: " + status.getStatus());
                }
            } else {
                Log.e("test", "Failed");
            }


        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("test", "onFailure");
        }
    };
    call.enqueue(callback);

now when run the app, onFailure happend but if main json just in onejsonArray and use just retroPost for parsing that thats work's very nice... what i mistake ?
sorry for bad english...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Root node of your json is not array, but you declared it as List<responseStatus> at interface getPost. So you need to use responseStatus instead current one. And use gettter to access data

@SerializedName("data")
private ArrayList<retroPost> data;

which is array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...