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

在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);





  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值