簡體   English   中英

如何使用 Retrofit 解析嵌套的 json ...?

[英]How to parse Nested json using Retrofit …?

我不知道如何使用 retrofit 解析 json 熟悉使用 Retrofit 解析簡單的 json 但不熟悉使用Retrofit解析嵌套的 Json

這是我的 Json 數據.......

  {
         "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"
                   }
             }
         }
     }

任何幫助將不勝感激。 謝謝。

這是我的簡單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);

            }

Retrofit使用以下Response

class Response{
   @SerializedName("current_observation")
   Observation observation;
   //getters and setters
}

class Observation{
   @SerializedName("image")
   Image image;
   //getters and setters
}

class Image{
  @SerializedName("title")
  String title;
  @SerializedName("link")
  String link;
  @SerializedName("url")
  String url;
  //getters and setters
}

CurrentObservation.class

public class CurrentObservation {

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

public Image1 getImage() {
return image;
}

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

例子.java

public class Example {

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

public CurrentObservation getCurrentObservation() {
return currentObservation;
}

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

圖像1.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
}

如果您正確實施 Pojo,這將更容易。

您的班級和您的 json 存在沖突。

從您的 Country 類中,“current_observation”將是List<Items> items;

那么你的 json 應該是這樣的:

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

如果使用 List,請注意方括號[] 即使只有 1 個項目,您的"current_observation"仍然需要為List<T>聲明這個。

我建議你使用這個網站: http : //www.jsonschema2pojo.org/

選擇Source Type: JSON , Annotation style: Moshi (I'm using Moshi, or you can use Gson), Tick on: Make class serialiable

它將為您的 json 生成正確的類。 您的其余代碼應該已經正確。

更新:如果生成類后,沒有生成列表,則不應使用

List<Items> items = response.body().getItems();

而是

Items items = response.body().getItems();

對於 JSON 解析,您應該使用JASONSCHEMA2POJO將 JSON 字符串轉換為模型類

在您的改造成功響應中

 CurrentObservation observation = new CurrentObservation  ();
 JSONObject jsonObject = new JSONObject(response.body().string());
 observation = new Gson().fromJson(jsonObject.getString("current_observation"),CurrentObservation .class);

模型類喜歡

public class Example {

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

public CurrentObservation getCurrentObservation() {
return currentObservation;
}

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

} 

我如何在主要活動中調用它?

你需要像這樣調用 pojo 類方法

String url=getCurrentObservation().getImage().getUrl();

如果你從回應中得到

String url=response.body().getCurrentObservation().getImage().getUrl();

如需更多幫助改造,請點擊此鏈接與我的 ans

我詳細查看了您的 Json 字符串。 只需查看您的“圖像”鍵,然后是 JSONObject。

{
     "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"
               }

     }
 }

由於您的“圖像”鍵有多個值,因此它應該在 JSONArray 中。 所以你的字符串應該看起來像

{
     "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"
               }
         ]

     }
 }

我建議你應該再次檢查你的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM