使用Gson转换json数据为Java对象的一个例子

记录工作中碰到的一个内容。

原料是微信平台的一个接口json数据。

{
    "errcode" : 0,
    "errmsg" : "ok",
    "business_list" : [{
            "base_info" : {
                "sid" : "",
                "business_name" : "xxxxxxxx服务中心",
                "branch_name" : "",
                "address" : "xx路8桃源山庄酒楼2层",
                "telephone" : "0771-56xxxx9",
                "categories" : ["公司企业,公司企业"],
                "city" : "xx市",
                "province" : "xxxx",
                "offset_type" : 1,
                "longitude" : 108.391288757,
                "latitude" : 22.8106937408,
                "photo_list" : [],
                "introduction" : "创新民营金融模式,服务区域经济发展",
                "recommend" : "",
                "special" : "",
                "open_time" : "08:30-17:30",
                "avg_price" : 0,
                "poi_id" : "465268573",
                "available_state" : 3,
                "district" : "青秀区",
                "update_status" : 0
            }
        }
    ],
    "total_count" : 1
}

封装的方法需要将这个数据转换为一个java对象作为返回值。

分析数据结构,先从最简单的开始。首先最里层的数据需要一个base_info辅助对象。

比如说是类StoreBaseInfo: 

package com.gxgrh.wechat.wechatapi.responseentity.store;

import com.google.gson.annotations.SerializedName;

import java.util.List;

/**门店信息基础类
 * Created by Administrator on 2016/12/7.
 */
public class StoreBaseInfo {

    private String sid;

    @SerializedName("business_name")
    private String businessName;

    @SerializedName("branch_name")
    private String branchName;

    private String address;

    private String telephone;

    private List<String> categories;

    private String city;

    private String province;

    @SerializedName("offset_type")
    private String offsetType;

    private float longitude;

    private float latitude;

    @SerializedName("photo_list")
    private List<PhotoUrl> photoList;


    private String recommend;

    private String special;

    private String introduction;

    @SerializedName("open_time")
    private String openTime;

    @SerializedName("avg_price")
    private float avgPrice;

    @SerializedName("available_state")
    private int availableState;


    @SerializedName("update_status")
    private int updateStatus;

    @SerializedName("poiId")
    private String poiId;

    private String district;

}

其次,business_list节点的数据是一个对象数组,所以需要针对数组中的对象再设计一个辅助类。

比如说是类StoreBaseInfoRsp:

import com.google.gson.annotations.SerializedName;

/**
 * Created by Administrator on 2016/12/7.
 */
public class StoreBaseInfoRsp {

    @SerializedName("base_info")
    private StoreBaseInfo storeBaseInfo;

    public StoreBaseInfo getStoreBaseInfo() {
        return storeBaseInfo;
    }

    public void setStoreBaseInfo(StoreBaseInfo storeBaseInfo) {
        this.storeBaseInfo = storeBaseInfo;
    }
}

然后就可以得到最终的类。

比如说是类StoreListRsp:

import com.google.gson.annotations.SerializedName;

import java.util.List;

/** 查询门店列表返回的数据
 * Created by Administrator on 2016/12/7.
 */
public class StoreListRsp {


    private String errcode;

    private String errmsg;

    @SerializedName("total_count")
    private int totalCount;

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    @SerializedName("business_list")
    private List<StoreBaseInfoRsp> businessList;
}

利用方法:

StoreListRsp storeListRsp = new Gson().fromJson(jsonResponse ,StoreListRsp.class);

就可以获得类对象了。记得引入Gson包。

原文地址:https://www.cnblogs.com/derry9005/p/6140953.html