GSON解析返回的json

1.依赖

//Gson
compile 'com.google.code.gson:gson:2.3.1'

2.创建一个bean类与json对应
json

{
  "code": 1,
  "msg": "OK",
   data": [
    {
      "name": "zhangsan",
      "age": "10",
      "phone": "11111",
      "email": "[email protected]"
    },
    {
      "name": "lisi",
      "age": "20",
      "phone": "22222",
      "email": "[email protected]"
    },
    ...
  ]
}

对应类

public class NewsResultBean
{  
    private int code;
    private String msg;
    private List data;
    
    public class NewsBean 
    {
        private int id;//文章id
        private int sid;//频道
        private int userid;
        private int updatetime;
        private int status;
        private int hits;
        private int createtime;
        private String username;
        private String title;
        private String thumb;
        private String content;
        private String description;
         ...
       }
    ...
}


3.返回json处理

//GSON直接解析成对象   

NewsResultBean newsResultBean = new Gson().fromJson(resultjson,NewsResultBean.class);

//对象中拿到集合           

final List newsBeanList = newsResultBean.getNewsBeanList();

//展示到UI中    

mNewsListView.setAdapter(new NewsAdapter(ActivityNewsList.this, newsBeanList));
                    

你可能感兴趣的:(GSON解析返回的json)