使用retrofit 2.0解析嵌套的json [英] Parse a nested json with retrofit 2.0

查看:352
本文介绍了使用retrofit 2.0解析嵌套的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JSON的,我想用改造解析它们。

  {
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,
用户名:admin,
coin:10 ,
datetime:1451982292,
isShow:1
}
]
}

当json处于数组模式时,我的代码工作。但我的问题是如何解析嵌套的json像上面的示例?



在这里我的javaClass:(statusClass)


@SerializedName(status)
private String status;

@SerializedName(data)
private ArrayList< retroPost>数据;

// getters And Setters
}

(retroPost类)

  public class retroPost {

@SerializedName(id)
private int ID;

@SerializedName(title)
私有字符串标题;

@SerializedName(text)
private String text;

@SerializedName(cat_id)
private int cat_id;

@SerializedName(datetime)
私有长日期时间;


// getters And Setters
}

(getPostInterface)

  public interface getPost {
@GET(get / posts / all)
呼叫<列表< retroStatus>>的getPost();

$ / code>

(和mainCode)

  Retrofit retrofit = new Retrofit.Builder()
.baseUrl(http://mywebsite.it/api/)
.addConverterFactory(GsonConverterFactory。 create())
.build();
getPost postService = retrofit.create(getPost.class);
呼叫<列表< retroStatus>> call = postService.getPost();
回拨<列表< 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);




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

现在运行应用程序时,onFailure发生,但是如果主json只是在onejsonArray中,并且使用retroPost进行解析那工作是非常好的...我的错误是什么?

抱歉,坏的英语......

解决方案

你的json的根节点不是数组,而是你在接口 getPost 中声明它为 List< responseStatus> 。所以你需要使用 responseStatus 来代替当前的。并使用gettter访问 data

  @SerializedName(data) 
private ArrayList< retroPost>数据;

这是数组

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...

解决方案

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

这篇关于使用retrofit 2.0解析嵌套的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆