怎样使用Gson 解析 json字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样使用Gson 解析 json字符串相关的知识,希望对你有一定的参考价值。

步骤1:目标:将从webservice传回的json


"status": 0,
"result":
"location":
"lng": 103.98964143811,
"lat": 30.586643130352
,
"formatted_address": "四川省成都市双流县北一街154",
"business": "簇桥,金花桥",
"addressComponent":
"city": "成都市",
"district": "双流县",
"province": "四川省",
"street": "北一街",
"street_number": "154"
,
"cityCode": 75


2
先普及下json数据格式定义: json数据只有两种格式.
一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对
"firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa"
一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象
如:

"people": [

"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
,

"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
,

"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"

]

END
步骤2 定义json数据格式对应的javaBean
1
public class Result
private Integer status;
private ResultDetail result;
public Result()

public Result(Integer status, ResultDetail result)
super();
this.status = status;
this.result = result;

public ResultDetail getResult()
return this.result;

public Integer getStatus()
return this.status;

public void setResult( ResultDetail result )
this.result = result;

public void setStatus( Integer status )
this.status = status;

@Override
public String toString()
return "Result [status=" + this.status + ", result=" + this.result
+ "]";


2
public class ResultDetail
Location location;
String formatted_address;
AddressComponent addressComponent;
String business;
String cityCode;
public ResultDetail()
super();
// TODO Auto-generated constructor stub

public ResultDetail(Location location, String formatted_address,
AddressComponent addressComponent, String business, String cityCode)
super();
this.location = location;
this.formatted_address = formatted_address;
this.addressComponent = addressComponent;
this.business = business;
this.cityCode = cityCode;

public AddressComponent getAddressComponent()
return this.addressComponent;

public String getBusiness()
return this.business;

public String getCityCode()
return this.cityCode;

public String getFormatted_address()
return this.formatted_address;

public Location getLocation()
return this.location;

public void setAddressComponent( AddressComponent addressComponent )
this.addressComponent = addressComponent;

public void setBusiness( String business )
this.business = business;

public void setCityCode( String cityCode )
this.cityCode = cityCode;

public void setFormatted_address( String formatted_address )
this.formatted_address = formatted_address;

public void setLocation( Location location )
this.location = location;


3
public class Location
String lng;
String lat;
public Location()

public Location(String lng, String lat)
this.lng = lng;
this.lat = lat;

public String getLat()
return this.lat;

public String getLng()
return this.lng;

public void setLat( String lat )
this.lat = lat;

public void setLng( String lng )
this.lng = lng;

@Override
public String toString()
return "Location [lng=" + this.lng + ", lat=" + this.lat + "]";


4
public class AddressComponent
String city;
String district;
String province;
String street;
String street_number;
public AddressComponent()
super();
// TODO Auto-generated constructor stub

public AddressComponent(String city, String district, String province,
String street, String street_number)
super();
this.city = city;
this.district = district;
this.province = province;
this.street = street;
this.street_number = street_number;

public String getCity()
return this.city;

public String getDistrict()
return this.district;

public String getProvince()
return this.province;

public String getStreet()
return this.street;

public String getStreet_number()
return this.street_number;

public void setCity( String city )
this.city = city;

public void setDistrict( String district )
this.district = district;

public void setProvince( String province )
this.province = province;

public void setStreet( String street )
this.street = street;

public void setStreet_number( String street_number )
this.street_number = street_number;

@Override
public String toString()
return "AddressComponent [city=" + this.city + ", district="
+ this.district + ", province=" + this.province + ", street="
+ this.street + ", street_number=" + this.street_number + "]";


5
测试:
jsonString ( 目标json数据,已经在最上面写好的)
System.out.println( "jsonString:" + jsonString );
Gson gson = new Gson();
Result fromJson = gson.fromJson( jsonString.toString() ,
Result.class );
System.out.println( "******************************************" );
System.out.println( fromJson );
6
结果:
jsonString:"status":0,"result":"location":"lng":103.98964143811,"lat":30.586643130352,"formatted_address":"四川省成都市双流县北一街154","business":"簇桥,金花桥","addressComponent":"city":"成都市","district":"双流县","province":"四川省","street":"北一街","street_number":"154","cityCode":75
*******************************************
Result [status=0, result=ResultDetail [location=Location [lng=103.98964143811, lat=30.586643130352], formatted_address=四川省成都市双流县北一街154, addressComponent=AddressComponent [city=成都市, district=双流县, province=四川省, street=北一街, street_number=154], business=簇桥,金花桥, cityCode=75]]
7
可见,jsonString已经成功的被转换成了对应的javaBean

步骤3 : 总结.说明
Gson可以很轻松的实现javaBean和jsonString之间的互转.只需要明白json如何定义.剩下的就非常简单了.
推荐使用该框架,在网上看到过一篇解析json的各种方式的效率比较. Gsoon的效率是最高的.
再推荐一个jsonString格式化工具.那样能方便你的阅读-地址可以在参考资料中找到

4
当然上面的例子只是Gson的很小的一个用例,对于将javabean转换成jsonString,以及更为复杂的使用.请关注我后续经验
参考技术A 步骤1:目标:将从webservice传回的json
1

"status": 0,
"result":
"location":
"lng": 103.98964143811,
"lat": 30.586643130352
,
"formatted_address": "四川省成都市双流县北一街154",
"business": "簇桥,金花桥",
"addressComponent":
"city": "成都市",
"district": "双流县",
"province": "四川省",
"street": "北一街",
"street_number": "154"
,
"cityCode": 75


2
先普及下json数据格式定义: json数据只有两种格式.
一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对
"firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa"
一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象
如:

"people": [

"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
,

"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
,

"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"

]

END
步骤2 定义json数据格式对应的javaBean
1
public class Result
private Integer status;
private ResultDetail result;

public Result()


public Result(Integer status, ResultDetail result)
super();
this.status = status;
this.result = result;


public ResultDetail getResult()
return this.result;


public Integer getStatus()
return this.status;


public void setResult( ResultDetail result )
this.result = result;


public void setStatus( Integer status )
this.status = status;


@Override
public String toString()
return "Result [status=" + this.status + ", result=" + this.result
+ "]";



2
public class ResultDetail
Location location;
String formatted_address;
AddressComponent addressComponent;
String business;
String cityCode;

public ResultDetail()
super();
// TODO Auto-generated constructor stub


public ResultDetail(Location location, String formatted_address,
AddressComponent addressComponent, String business, String cityCode)
super();
this.location = location;
this.formatted_address = formatted_address;
this.addressComponent = addressComponent;
this.business = business;
this.cityCode = cityCode;


public AddressComponent getAddressComponent()
return this.addressComponent;


public String getBusiness()
return this.business;


public String getCityCode()
return this.cityCode;


public String getFormatted_address()
return this.formatted_address;


public Location getLocation()
return this.location;


public void setAddressComponent( AddressComponent addressComponent )
this.addressComponent = addressComponent;


public void setBusiness( String business )
this.business = business;


public void setCityCode( String cityCode )
this.cityCode = cityCode;


public void setFormatted_address( String formatted_address )
this.formatted_address = formatted_address;


public void setLocation( Location location )
this.location = location;



3
public class Location
String lng;
String lat;

public Location()



public Location(String lng, String lat)
this.lng = lng;
this.lat = lat;


public String getLat()
return this.lat;


public String getLng()
return this.lng;


public void setLat( String lat )
this.lat = lat;


public void setLng( String lng )
this.lng = lng;


@Override
public String toString()
return "Location [lng=" + this.lng + ", lat=" + this.lat + "]";



4
public class AddressComponent
String city;
String district;
String province;
String street;
String street_number;

public AddressComponent()
super();
// TODO Auto-generated constructor stub


public AddressComponent(String city, String district, String province,
String street, String street_number)
super();
this.city = city;
this.district = district;
this.province = province;
this.street = street;
this.street_number = street_number;


public String getCity()
return this.city;


public String getDistrict()
return this.district;


public String getProvince()
return this.province;


public String getStreet()
return this.street;


public String getStreet_number()
return this.street_number;


public void setCity( String city )
this.city = city;


public void setDistrict( String district )
this.district = district;


public void setProvince( String province )
this.province = province;


public void setStreet( String street )
this.street = street;


public void setStreet_number( String street_number )
this.street_number = street_number;


@Override
public String toString()
return "AddressComponent [city=" + this.city + ", district="
+ this.district + ", province=" + this.province + ", street="
+ this.street + ", street_number=" + this.street_number + "]";



5
测试:
jsonString ( 目标json数据,已经在最上面写好的)
System.out.println( "jsonString:" + jsonString );
Gson gson = new Gson();
Result fromJson = gson.fromJson( jsonString.toString() ,
Result.class );
System.out.println( "******************************************" );
System.out.println( fromJson );
6
结果:
jsonString:"status":0,"result":"location":"lng":103.98964143811,"lat":30.586643130352,"formatted_address":"四川省成都市双流县北一街154","business":"簇桥,金花桥","addressComponent":"city":"成都市","district":"双流县","province":"四川省","street":"北一街","street_number":"154","cityCode":75
*******************************************
Result [status=0, result=ResultDetail [location=Location [lng=103.98964143811, lat=30.586643130352], formatted_address=四川省成都市双流县北一街154, addressComponent=AddressComponent [city=成都市, district=双流县, province=四川省, street=北一街, street_number=154], business=簇桥,金花桥, cityCode=75]]
7
可见,jsonString已经成功的被转换成了对应的javaBean
参考技术B   Gson是谷歌推出的解析json数据以及将对象转换成json数据的一个开源框架. 现在json因其易读性和高效率而被广泛的使用着.
  相对于java以及其它json的解析框架,Gson非常的好用.
  简单来讲就是根据json的数据结构定义出相应的javabean --->"new"出Gson的实例gson---->gson.fromJson(jsonString,JavaBean.class) 即可.转载,仅供参考。本回答被提问者和网友采纳

Gson解析json字符串

Gson

怎样使用gson把一个json字符串解析成一个jsonObject对象

因此我要把上面的fastjson转换成是gson,如下图:

JsonObject object = new JsonParser().parse(result).getAsJsonObject();

怎样从gson中取出键的值

使用gson把json字符串转换成一个list集合

List<ComplaintWeiXin> complaintWeiXinList = new Gson().fromJson(jsonString, new TypeToken<List<ComplaintWeiXin>>() .getType());

使用gson把json数组字符串转换成list集合的时候得到的集合中每个元素的属性都是null

为什么使用gson转换的时候会出现这个问题呢?使用fastjson转换的时候怎么不会出现这个问题呢?首先来看一下我们从微信接口获取到的返回数据,如下图:

如果我们用fastjson将这个json字符串数组转换成list集合,它是可以自动进行下划线转驼峰的。但是如果我们使用gson将这个json字符串数组转换成list集合,它是不会自动进行下划线转驼峰的,需要我们在java对象中手动指定,如下图:

json数组字符串转换成jsonArray数组

new JsonParse().parse(str).getAsJsonArray()



以上是关于怎样使用Gson 解析 json字符串的主要内容,如果未能解决你的问题,请参考以下文章

Gson解析json字符串

java怎么使用gson解析json字符串

使用gson解析怎么将json字符串解析为数组

利用GSON解析简单Json字符串

最佳实践 - Json 解析中的字符串与 InputStream(使用 gson)

使用 GSON 不解析字段,只保留 json 原始字符串