search

Home  >  Q&A  >  body text

android - 类似这样的JSON该怎么解析?(用Gson)

如果我想用里面的stories数组,要怎么解析?
我定义好了一个News类和Story、TopStory两个内部类,然后写了下面的代码,不行。

Gson gson = new Gson();
storiesList = gson.fromJson(JSONData, new TypeToken<List<News.Story>>(){}.getType());

刚学,网上搜也没找到解决

PHP中文网PHP中文网2773 days ago312

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 18:01:22

        public class Story {
            String title;
            String[] image;
            String ga_prefix;
            int type;
            int id;
        
            //setter & getter
        }
        
        public class Response {
            String date;
            List<Strory> stories, top_stories;
        
            //setter & getter
        }
        
        Goson goson = new Goson()
        Response res = goson.fromJson(jsonData, Response.class);
    

    It’s done with just two classes, the above is the pseudo code

    reply
    0
  • Cancelreply