本文目录一览:
Gson 在java 如何使用啊???
google的gson,心想google的应该不会差,带着一些好奇心,我开始使用了gson。
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
这里提供gson-2.2.4.jar的下载(密码:4Sk4)
下面是一个简单的例子:
//一个 Person 实体
public class Person {
private String name;
private int age;
//以下是get,set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
实体很简单,两个字段,当然实体中的字段也可以是List或者Set类型的。
Gson gson = new Gson();
ListPerson persons = new ArrayListPerson();
for (int i = 0; i 10; i++) {
Person p = new Person();
p.setName("name" + i);
p.setAge(i * 5);
persons.add(p);
}
String str = gson.toJson(persons);
上面的代码重点是Gson对象,它提供了toJason()方法将对象转换成Json字符串,上面代码的str对象值为:
[
{"name":"name0","age":0},
{"name":"name1","age":5},
{"name":"name2","age":10},
{"name":"name3","age":15},
{"name":"name4","age":20},
{"name":"name5","age":25},
{"name":"name6","age":30},
{"name":"name7","age":35},
{"name":"name8","age":40},
{"name":"name9","age":45}
]
很标准的json数据,很简单吧,呵呵。
下面来看看gson的反序列化,Gson提供了fromJson()方法来实现从Json相关对象到java实体的方法。
在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构。
先来看第一种:
比如json字符串为:[{"name":"name0","age":0}]
Person person = gson.fromJson(str, Person.class);
提供两个参数,分别是json字符串以及需要转换对象的类型。
java怎么使用gson解析json字符串
Gson是谷歌推出的解析json数据以及将对象转换成json数据的一个开源框架. 现在json因其易读性和高效率而被广泛的使用着.
相对于java以及其它json的解析框架,Gson非常的好用.
简单来讲就是根据json的数据结构定义出相应的javabean ---"new"出Gson的实例gson----gson.fromJson(jsonString,JavaBean.class) 即可.
下面给出一个实例来说明.
步骤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
}
}
先普及下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"
}
]
}
步骤2 定义json数据格式对应的javaBean
public class Result {
private Integerstatus;
private ResultDetailresult;
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
+ "]";
}
}
public class ResultDetail {
Locationlocation;
Stringformatted_address;
AddressComponentaddressComponent;
Stringbusiness;
StringcityCode;
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;
}
}
public class Location {
Stringlng;
Stringlat;
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 + "]";
}
}
public class AddressComponent {
Stringcity;
Stringdistrict;
Stringprovince;
Stringstreet;
Stringstreet_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 + "]";
}
}
测试:
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 );
结果:
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]]
可见,jsonString已经成功的被转换成了对应的javaBean
步骤3 : 总结.说明
Gson可以很轻松的实现javaBean和jsonString之间的互转.只需要明白json如何定义.剩下的就非常简单了.
JAVA,为什么我不能引用gson包?
gson是第三方包,最好的方式是你在你的IDE里面引入,我不知道你用的是eclipse还是idea,如果是idea,打开你当前项目的project Structure选项,点击最右侧的加号,
然后在你的本地目录中选择具体的jar包,
如果你用的是eclipse,也是一样的,要去设置你的项目依赖jar包,这些网上都可以搜到的。