gpt4 book ai didi

android - 用于嵌套 json 的 POJO

转载 作者:行者123 更新时间:2023-11-29 01:56:23 25 4
gpt4 key购买 nike

我是 JSON 的新手,在为后续的 JSON 输入创建 POJO 时遇到了问题。

    [
{
"score":1,
"popularity":3,
"name":"Brad Pitt",
"id":287,
"biography":"test",
"url":"http://www.themoviedb.org/person/287",
"profile":[
{
"image":{
"type":"profile",
"size":"thumb",
"height":68,
"width":45,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"profile",
"height":281,
"width":185,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"h632",
"height":632,
"width":416,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"original",
"height":1969,
"width":1295,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
}
],
"version":685,
"last_modified_at":"2013-02-16 07:11:15 UTC"
}
]

我已经创建了三个 POJO,其中一个是主要的 person 类,这个也包含配置文件对象。配置文件对象返回图像列表。:

    public class Person implements Serializable {

private static final long serialVersionUID = 6794898677027141412L;

public String biography;
public String id;
public String last_modified_at;
public String name;
public String popularity;
public String score;
public String url;
public String version;
public ArrayList<Profile> profile;

//getters and setters
}

public class Profile implements Serializable {

private static final long serialVersionUID = -6482559829789740926L;

public ArrayList<Image> image;

}


public class Image implements Serializable {

private static final long serialVersionUID = -2428562977284114465L;

public String type;
public String url;
public String size;
public int width;
public int height;
}

我正在使用 gson 检索数据:

  Gson gson = new Gson();
Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person> details = gson.fromJson(json, collectionType);

当我运行它时我得到:

  02-17 11:04:50.531: E/AndroidRuntime(405): Caused by:  com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3

更新图像 POJO:

public class Image implements Serializable {

private static final long serialVersionUID = -2428562977284114465L;
private Object image;

public class ImageInner {

public static final String SIZE_ORIGINAL = "original";
public static final String SIZE_MID = "mid";
public static final String SIZE_COVER = "cover";
public static final String SIZE_THUMB = "thumb";
public static final String TYPE_PROFILE = "profile";
public static final String TYPE_POSTER = "poster";
public String type;
public String url;
public String size;
public int width;
public int height;

}
}

谢谢

最佳答案

您的代码实际上非常接近,有两个错误:

1) 您收到的错误告诉您,在 JSON 的最开始它期待数组的开始 ([) ,但得到了一个对象的开始 ({) 代替。您发布的 JSON 不是您提供给 Gson 的内容(见下文)。

2) 当您更正上述内容时,您的 Person POJO 中出现了一个小问题:

public ArrayList<Profile> profile;

应该是:

public ArrayList<Image> profile;

您实际上不需要您的Profile 类; JSON 中的profile一个数组。按原样,您会收到另一个与您现在收到的错误类似的错误,因为它需要一个包含 Profile 对象的 ArrayList:"profile":[{"image":[{imageobject },...]},{"图像":[{imageobject},...]}]

如果您修复第二个问题并将您发布的 JSON 提供给 Gson,它会完美运行。您已经发布了一组 Person 对象,但您在代码中(在 json 中)实际拥有的只是一个对象......或包含该数组的对象你已经发布了。下面的小例子完美地工作(在更正你的 Person 类之后):

public class App
{
static String json = "[{\"score\":1,\"popularity\":3,\"name\":\"Brad Pitt\",\"id\":287," +
"\"biography\":\"test\",\"url\":\"http://www.themoviedb.org/person/287\",\"profile\":" +
"[{\"image\":{\"type\":\"profile\",\"size\":\"thumb\",\"height\":68,\"width\":45,\"url\":" +
"\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"profile\"," +
"\"height\":281,\"width\":185,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"h632\",\"height\":632,\"width\":416,"+
"\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"original\",\"height\":1969," +
"\"width\":1295,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}}],\"version\":685,\"last_modified_at\":\"2013-02-16 07:11:15 UTC\"" +
"}]";

public static void main(String[] args)
{

Gson gson = new Gson();
Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person> details = gson.fromJson(json, collectionType);
}
}

编辑以添加:您实际上正在进行一些 Gson 正在为您处理的类型转换……但您可能并不真正想要。例如,在您的 JSON 中,score 是一个 int,但在您的 POJO 中,您已将其设为 String。 Gson 默默地进行转换。

此外,像我上面所做的那样简单地创建一个 SSCCE ( http://sscce.org/) 可以帮助您调试这些问题。

关于android - 用于嵌套 json 的 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920495/

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