gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:40:07 24 4
gpt4 key购买 nike

我不知道如何使用改造解析 json。熟悉使用 Retrofit 解析简单的 json,但不熟悉使用 Retrofit 解析 nested 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;
}
}

主要 Activity 中的代码

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

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;
}

}

在主Activity中调用

  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
}

关于android - 如何使用 Retrofit 解析嵌套的 json ....?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com