无法使用Gson解析复杂的Json

时间:2014-12-14 06:56:20

标签: android json parsing gson

我正在处理来自服务器的以下响应,我需要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();
    }

}

图像课程如下:

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

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

1 个答案:

答案 0 :(得分:1)

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

解析每个数据。

enter image description here