如何使用Retrofit ....解析嵌套的json? [英] How to parse Nested json using Retrofit ....?

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

问题描述

我不知道如何使用翻新解析json .熟悉使用Retrofit解析简单json,但不熟悉使用 Retrofit 解析嵌套Json .

I don't know how to parse json using retrofit. Am familiar with parsing simple json using Retrofit but am not familiar with parsing nested Json using Retrofit.

这是我的Json数据........

Here is my Json data.................

  {
         "current_observation": {
             "image": {
                 "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",                
                 "title":"Weather Underground",
                 "link":"http://www.wunderground.com"
},
                  {
                  "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",               
                 "title":"Weather Underground",
                 "link":"http://www.wunderground.com"
                   }
             }
         }
     }

任何帮助将不胜感激. 谢谢.

Any help will be appreciated. Thank you.

这是我用于简单json的方法

This is my method for simple json

public class Country {
    @SerializedName("current_observation")
    @Expose
    private List<Items> items;

    public List<Items> getItems() {
        return items;
    }

    public void setItems (List<Items> items) {
       this.items = items;
    }
}

这是.....

 public class Items {
        @SerializedName("image")
        @Expose
        private String url;
        private String title;
        private String link;

        public String getFlag() {
            return url;
        }
        public String getRank() {
           return link;
        }
        public void setRank(String link) {
            this.link = link;
        }
        public void setFlag(String url) {
            this.url = url;
        }
        public String getCountryname() {
            return title;
        }
        public void setCountryname(String rating) {
            this.title = rating;
        }
    }

主要活动中的代码

Call <Country>  call = apiInterface.getCountries();

        call.enqueue(new Callback <Country>() {
            @Override
            public void onResponse(Call<Country> call, Response<Country>  response) {
                Log.d(TAG,"onSuccess Server Response "+ response.toString());

                Log.d(TAG,"onSuccess received information "+ response.body().toString());
                List<Items> items = response.body().getItems();
                adapter = new RecAdapter(items, getContext().getApplicationContext());
                recyclerView.setAdapter(adapter);

            }

推荐答案

CurrentObservation.class

CurrentObservation.class

public class CurrentObservation {

@SerializedName("image")
@Expose
private Image1 image;

public Image1 getImage() {
return image;
}

public void setImage(Image1 image) {
this.image = image;
}
}

Example.java

Example.java

public class Example {

@SerializedName("current_observation")
@Expose
private CurrentObservation currentObservation;

public CurrentObservation getCurrentObservation() {
return currentObservation;
}

public void setCurrentObservation(CurrentObservation currentObservation) {
this.currentObservation = currentObservation;
}
}

Image1.java

Image1.java

public class Image1 {

@SerializedName("url")
@Expose
private String url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("link")
@Expose
private String link;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

}

在主活动中调用它

  Call<Example> ex = BaseUrlClass.getInterface().ex("whatever parameters");
    ex.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {
            Example list = response.body();

            CurrentObservation a = list.getCurrentObservation();
            List<Image1> im = a.getImage();
            for (int i = 0;i<im.size();i++){
                Image1 image1= im.get(i);
                String a = image1.getTitle();
                String b = image1.getUrl();
                String c = image1.getLink();
            }
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

        }
    });

几乎在您的界面中

public interface ApiUtils {

@GET("")  //whatever url
Call<Example> ex();  //or any parameter
}

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

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