【问题标题】:How to parse Nested json using Retrofit ....?如何使用 Retrofit .... 解析嵌套的 json?
【发布时间】:2018-07-12 19:19:02
【问题描述】:

我不知道如何使用改造来解析 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;
        }
    }

主活动中的代码

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

            }

【问题讨论】:

  • 不需要解析JSON对象,在模型类中使用注解
  • 向我们展示您如何处理您的“简单 json”,然后我们将建议如何修改
  • 是的,我会展示它。 @Jacky
  • @farhana 是的,我知道。我的简单 json 代码将显示更多亮点
  • @Jacky 请通过我的代码

标签: android arrays json retrofit


【解决方案1】:

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

} 

【讨论】:

  • @MilkaMozhi 答案有什么复杂的?
  • 对不起,兄弟,在看到您编辑的帖子之前我已经回复了
【解决方案2】:

Response 类下面使用Retrofit

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
}

【讨论】:

  • 是的,谢谢兄弟......我怎么能在主要活动中调用它......??你能给我一些样本吗?
  • @MilkaMozhi 这是示例链接pratikbutani.com/2016/05/…。希望对你有帮助
  • 发布你的整个 Json ,是对象还是数组?
  • 等哥我试试
  • 我已经发布了我的整个 json 兄弟
【解决方案3】:

如果你正确实现 Pojo,这会更容易。

你的班级和你的 json 有冲突。

在您的 Country 类中,“current_observation”将是 List&lt;Items&gt; 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&lt;T&gt; 声明。

我建议你使用这个网站:http://www.jsonschema2pojo.org/

选择Source Type: JSONAnnotation style: Moshi(我用的是Moshi,也可以用Gson),勾选:Make class serialiable

它将为您的 json 生成正确的类。您的其余代码应该已经正确。

更新: 如果生成类后没有生成List,则不要使用

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

而是

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

【讨论】:

  • 是的,你正确的兄弟......它的列表......不是一个。我对其进行了修剪以简化问题......我现在已经更新了它。请仔细阅读它
  • @MilkaMozhi 还是错了我的朋友,使用 json 验证器,你可以看到它。这就是为什么我怀疑您的 json 无法序列化到您的班级
【解决方案4】:

如何在主要活动中调用它?

你需要像这样调用pojo类方法

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

如果你收到回复

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

如需更多改装帮助,请关注this link 和我的回答

【讨论】:

    【解决方案5】:

    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

    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

    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
    }
    

    【讨论】:

    • 它给出的错误是什么。你试过调试吗?
    【解决方案6】:

    我详细查看了您的 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"
                   }
             ]
    
         }
     }
    

    我建议你应该再次检查你的字符串。

    【讨论】:

      猜你喜欢
      • 2015-04-07
      • 2021-02-03
      • 2015-09-12
      • 2016-01-01
      • 2018-05-31
      • 2019-07-30
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多