Json封装到对象

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by lzd on 2016年6月22日 下午6:29:32  
 */
public class Test {

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Top top = Top.top();
                System.out.println(top.getBody().getDepartureList("A").size());
                System.out.println(top.getBody().getHotCityList().length);
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                Top top = Top.top();
                System.out.println(top.getBody().getDepartureList("A").size());
                System.out.println(top.getBody().getHotCityList().length);
            }
        }).start();
    }
}

class Top implements Serializable{
    private static final long serialVersionUID = 1L;
    
    private String header;
    private Body body;
    private static volatile Top top = null;

    public static Top top(){
        if(top == null){
                synchronized (Top.class) {
                    if(top == null){
                        String res = "";
                        try {
                            res = FileUtils.readFileToString(new File("D:\\yaoyongbo.txt"),"gbk");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Map map = JSON.parseObject(res,Map.class);
                        Object obj = map.get("response");
                        String str = String.valueOf(obj);
                        top = JSON.parseObject(str,Top.class);
                    }
                }
            }
        return top;
    }
    
    private Top(){}
    public String getHeader() {
        return header;
    }
    public void setHeader(String header) {
        this.header = header;
    }
    public Body getBody() {
        return body;
    }
    public void setBody(Body body) {
        this.body = body;
    }
}

class Body implements Serializable{
    
    private static final long serialVersionUID = 7910832368022336947L;

    private City[] hotCityList;
    private Map[] departureList;
    private String isSuccess;
    private String description;
    private String code;
    
    //存放departureList中的值,prefix为key,cities为value
    private final Map<String, List<City>> map = new HashMap<String, List<City>>();
    
    public City[] getHotCityList() {
        return hotCityList;
    }
    public void setHotCityList(City[] hotCityList) {
        this.hotCityList = hotCityList;
    }
    public Map[] getDepartureList() {
        return departureList;
    }
    public List<City> getDepartureList(String key) {
        return this.map.get(key);
    }
    public void setDepartureList(Map[] departureList) {
        this.departureList = departureList;
        
        if(this.departureList != null && this.departureList.length > 0 ){
            for (Map map : departureList) {
                Object objKey = map.get("prefix");
                JSONArray arrVal = (JSONArray) map.get("cities");
                List<City> list = new ArrayList<>(arrVal.size());
                for (int i = 0; i < arrVal.size(); i++) {
                    JSONObject jsonObject = arrVal.getJSONObject(i);
                    City city = JSON.parseObject(jsonObject.toJSONString(),City.class);
                    list.add(city);
                }
                this.map.put((String)objKey,list);
            }
        }
    }
    public String getIsSuccess() {
        return isSuccess;
    }
    public void setIsSuccess(String isSuccess) {
        this.isSuccess = isSuccess;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

class City implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private String enName; 
    private String shortEnName;
    private String prefixLetter; 
    private String type;
    private String enabled; 
    private String searchName;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEnName() {
        return enName;
    }
    public void setEnName(String enName) {
        this.enName = enName;
    }
    public String getShortEnName() {
        return shortEnName;
    }
    public void setShortEnName(String shortEnName) {
        this.shortEnName = shortEnName;
    }
    public String getPrefixLetter() {
        return prefixLetter;
    }
    public void setPrefixLetter(String prefixLetter) {
        this.prefixLetter = prefixLetter;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getEnabled() {
        return enabled;
    }
    public void setEnabled(String enabled) {
        this.enabled = enabled;
    }
    public String getSearchName() {
        return searchName;
    }
    public void setSearchName(String searchName) {
        this.searchName = searchName;
    }
}

 

json连接:http://share.weiyun.com/36e4eff1e789392e730856896111b9d9

posted @ 2016-06-23 10:37  姩澕  阅读(809)  评论(0编辑  收藏  举报