Gson使用方法

对于json数据,我们需要进行解析并封装为对象,Gson只需要传入空对象(包含所有字段),就可以将json字符串转化为modal,同时,modal也可以通过Gson转变为json数据。

1、json转对象:

该方法通过Xutils从服务器端获取数据并解析封装为对象:其中orderList为集合,对象ListView中adapter的数据。

private void getDataList() {

        datas = new ArrayList<OrderItem>();
        RequestParams params = new RequestParams();
        params.addHeader("Authorization", "Bearer " + Constants.access_token);
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, Constants.baseURL
                + "farmers/" + Constants.userID + "/orders?page=2", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.e("=====json 获取数据失败", arg1);

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> response) {
                        Log.e("我的订单=====json数据", response.result);
                        Gson gson = new Gson();
                        OrderList bean = gson.fromJson(response.result,
                                OrderList.class);
/*                      String json = gson.toJson(bean);
                        Log.e("我的订单=====转json ok", json);*/
                        datas = bean.content;
                        setAdapter();
                        Log.e("我的订单=====解析成功", response.result);
                    }
                });
    }

原json数据:

{
    "content": [ { "items": [ { "serviceName": "葛淑英创建的" } ], "orderNum": 2865023853, "putOrderTime": "2015-08-01 14:47:12", "state": "已取消" }, { "items": [ { "serviceName": "为黄氏而建" } ], "orderNum": 3834137140, "price": "¥ 12300.00", "putOrderTime": "2015-08-01 14:44:09", "state": "已完成" }, { "items": [ { "serviceName": "为黄氏而建" } ], "orderNum": 2930952581, "putOrderTime": "2015-08-01 13:58:35", "state": "已取消" }, { "items": [ { "serviceName": "播种" }, { "serviceName": "测土" } ], "orderNum": 4828959701, "putOrderTime": "2015-08-01 13:03:33", "state": "已取消" }, { "items": [ { "serviceName": "播种" } ], "orderNum": 4275681450, "price": "¥ 1230.00", "putOrderTime": "2015-06-24 10:48:18", "state": "已完成" } ],
    "first": false,
    "last": true,
    "number": 2,
    "numberOfElements": 5,
    "size": 10,
    "totalElements": 25,
    "totalPages": 3 }

对应的bean对象:

orderList.java

package com.irelint.stanleyagricultural.domain;

import java.util.List;

public class OrderList {

    public List<OrderItem> content;

    public class OrderItem{
        public List<OrderDes> items;
        public String orderNum;
        public String putOrderTime;
        public String state;
        public String price;
    }
    public class OrderDes{
        public String serviceName;
        public String imgUrl;
    }

}

如果json数据是集合而不是对象,则可以按如下方案(其中FarmerService封装的是集合中的对象):

Type type = new TypeToken<LinkedList<FarmerService>>(){}.getType();
Gson gson = new Gson();
private LinkedList<FarmerService> farmerServices = gson.fromJson(response.result, type);

你可能感兴趣的:(gson)