2

我只是尝试使用 GSON 解析给定的 URL,但遇到了一些问题。

网址

http://api.flickr.com/services/feeds/photos_public.gne?format=json

jsonFlickrFeed({
    "title": "Uploads from everyone",
    "link": "http://www.flickr.com/photos/",
    .....)

Json 响应是这样的。其中包含一个 ({ 在字符串的开头,这会导致解析出现问题。

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

我的模型类如下所示:

 public class Flicker {

    public JsonFlickrFeed jsonFlickrFeed;

    public JsonFlickrFeed getDealList() {

        return jsonFlickrFeed;
    }

    public static class JsonFlickrFeed{

        public String title;
        public String link;
        public String description;
        public String modified;
        public String generator;
        public List<Items> items = new ArrayList<Flicker.Items>();

    } 

    public static class Items {

        public String title;
        public String link;
        public String media;
        public String date_taken;
        public String description;
        public String published;
        public String author;
        public String  author_id;
        public String  tags;
    }


}
4

1 回答 1

2

看一下Json,您可以看到这media是类:

"media": {"m":"http://farm4.staticflickr.com/3706/10153214694_177e2dcd6d_m.jpg"},

因此我添加了新Media课程String m;

顺便说一句,你不需要发起List<Items> items = new ArrayList<Flicker.Items>();

Gson 为您执行此操作并将任何转换ListLinkedList

这是一个带有修复的工作代码:

启动器

str = str.substring("jsonFlickrFeed(".length(), str.length()-1);

Gson gson = new Gson();
System.out.println(str);
JsonFlickrFeed jj = gson.fromJson(str, JsonFlickrFeed.class);

System.out.println((jj != null)?"true":"false");

媒体

public class Media {
    public String m;
}

JsonFlickrFeed

public class JsonFlickrFeed {
     public String title;
     public String link;
     public String description;
     public String modified;
     public String generator;
     public List<Items> items;
}

项目

public class Items {
    public String title;
    public String link;
    public Media media;
    public String date_taken;
    public String description;
    public String published;
    public String author;
    public String  author_id;
    public String  tags;
}
于 2013-10-08T11:00:05.903 回答