【问题标题】:Unable to Parse a complex Json using Gson无法使用 Gson 解析复杂的 Json
【发布时间】:2014-12-14 06:56:20
【问题描述】:

我正在处理来自服务器的以下响应,我需要 Gson 来解析这个不太常见的响应:

 [  
   {  
      "body":"Some Text",
      "images":[  
         {  
            "height":284,
            "top_image":true,
            "url":"http://www.google.com",
            "width":450
         }
      ],
      "title":"Wow"
   },
   {  
      "body":"Some Text",
      "images":[  
         {  
            "height":200,
            "top_image":true,
            "url":"http://www.url.com",
            "width":600
         }
      ],
      "title":"Voom"
   },
   {  
      "body":"Some Text",
      "images":[  
         {  
            "height":360,
            "top_image":false,
            "url":"http://mobile.com",
            "width":190
         },
         {  
            "height":262,
            "top_image":true,
            "url":"http://noidea.com",
            "width":480
         }
      ],
      "title":"Quarry"
   }
]

我已经按如下方式设置了getter和setter:

public class Articles {

    public List<Articles> results;

    @SerializedName("body")
    private String body;
    @SerializedName("title")
    private String title;
    @SerializedName("images")
    private List<Images> images

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

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

    public List<Images> getImages() {
        return images ;
    }

    public void setImages(List<Images> images) {
        this.images = images;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("***** Articles  *****\n");
        sb.append("BODY=" + getBody() + "\n");
        sb.append("TITLE=" + getTitle() + "\n");
        sb.append("IMAGES=" + getImages() + "\n");
        sb.append("*****************************");

        return sb.toString();
    }

}

Images 类如下:

public class Images {

    private String width;
    private String height;
    private String url;
    private String topImage;

    public String getWidth() {
        return width;
    }
    public void setWidth(String width) {
        this.width = width;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getTopImage() {
        return topImage;
    }
    public void setTopImage(String topImage) {
        this.topImage = topImage;
    }

    @Override
    public String toString(){
        return getWidth() + ", "+getHeight()+", "+getUrl()+", "+getTopImage();
    }
}

我试过这个:

Gson gson = new Gson();

Articles[] testCase = gson.fromJson(jsonString, Articles[].class);

我需要一些关于如何使用最佳实践来解析这个(对我来说很复杂)响应的建议,任何帮助将不胜感激。如果我遗漏了任何拼图,请告诉我。

【问题讨论】:

  • 您的回复中有一组图像。所以我认为你应该使用private List&lt;Images&gt; images
  • @MisaghEmamverdi 我同意我将编辑代码,但就响应而言,您是否知道要完全解析它?

标签: android json parsing gson


【解决方案1】:

@MR Mido:您的代码运行良好。 我只是用最新的 GSON lib 进行测试。 (2.2.4)

每个数据都被解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2013-09-02
    • 2023-03-25
    相关资源
    最近更新 更多