json数据解析,json数据转为java对象

来源:互联网 发布:京东秒杀抢购软件 编辑:程序博客网 时间:2024/06/06 03:49
在Android开发过程中,经常需要与后台进行数据的交互,JSON作为一种轻量级的数据格式,经常被

后台作为传输数据的格式,将数据传输到客户端。JSON有两种格式,一种是对象格式的,另一种是数组格式的。

下面是一组json字符串:

String json="{"resultcode":"200","reason":"Return Successd!","result":{"province":"北京","city":"","areacode":"010","zip":"100000","company":"联通","card":""},"error_code":0}"

1.我们按json原生解析的方法一步一步进行解析:

JSONObject jsonObject = new JSONObject(json);        JSONObject object = jsonObject.getJSONObject("result");String province=object.getString("province");String city=object.getString("city");String areacode=object.getString("areacode");

2.将json转换为java对象:首先根据json写出对应的实体类:

public class Root<T> {private String resultcode;private String reason;private T result;private int error_code;public void setResultcode(String resultcode){this.resultcode = resultcode;}public String getResultcode(){return this.resultcode;}public void setReason(String reason){this.reason = reason;}public String getReason(){return this.reason;}public void setResult(T result){this.result = result;}public T getResult(){return this.result;}public void setError_code(int error_code){this.error_code = error_code;}public int getError_code(){return this.error_code;}}
public class result {    private String province;    private String city;    private String areacode;    private String zip;    private String company;    private String card;    public String getProvince() {        return province;    }    public void setProvince(String province) {        this.province = province;    }    public String getCity() {        return city;    }    public void setCity(String city) {        this.city = city;    }    public String getZip() {        return zip;    }    public void setZip(String zip) {        this.zip = zip;    }    public String getAreacode() {        return areacode;    }    public void setAreacode(String areacode) {        this.areacode = areacode;    }    public String getCompany() {        return company;    }    public void setCompany(String company) {        this.company = company;    }    public String getCard() {        return card;    }    public void setCard(String card) {        this.card = card;    }}

转换:

JSONObject jsonObject=JSONObject.fromObject(json);Root root=(Root)JSONObject.toBean(jsonObject, Root.class);

使用FastJson进行转换:

Root<JSON> root=JSONObject.parseObject(json,Root.class);        Result result =JSONObject.toJavaObject(root.getResult(),Result.class);
使用GSON进行转换:对于有泛型引入的,需要多写一句话用于获取泛型信息。

Gson gson=new Gson();        Type userType = new TypeToken<Root<Result>>(){}.getType();//用于获取泛型信息        Root<Result> root=gson.fromJson(json, userType);Result result=root.getResult();


然后,下面一段json字符串中有数组内容:
String json="{"resultcode":"200","reason":"Return Successd!","result":[{"province":"北京","city":"","areacode":"010","zip":"100000","company":"联通","card":""},{"province":"北京","city":"","areacode":"010","zip":"100000","company":"联通","card":""}],"error_code":0}"

使用GSON进行转换:
Gson gson=new Gson();        Type userType = new TypeToken<Root<List<Result>>>(){}.getType();        Root<List<Result>> root=gson.fromJson(json, userType);        List<Result> result=root.getResult();
使用FastJson进行转换:
Root<JSON> root=JSONObject.parseObject(json,Root.class);        List<Result> result =JSONObject.parseArray(root.getResult().toJSONString(),Result.class);

将json数组转换为list对象:

String str="[{"name":"真实服务器","ip":"101.37.168.121","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":false},        {"name":"模拟服务器","ip":"101.37.34.221","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":true}]";
使用GSON进行转换:
Gson gson=new Gson();Service[] array = gson.fromJson(str, Service[].class);List<Service> service=Arrays.asList(array);
使用FastJson进行转换:

List<Service> service=JSONArray.parseArray(str, Service.class);





1 0