导航:首页 > 编程语言 > jasonjava

jasonjava

发布时间:2023-06-12 16:36:30

A. 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


publicclassResult{
privateIntegerstatus;
privateResultDetailresult;
publicResult(){
}
publicResult(Integerstatus,ResultDetailresult){
super();
this.status=status;
this.result=result;
}
publicResultDetailgetResult(){
returnthis.result;
}
publicIntegergetStatus(){
returnthis.status;
}
publicvoidsetResult(ResultDetailresult){
this.result=result;
}
publicvoidsetStatus(Integerstatus){
this.status=status;
}
@Override
publicStringtoString(){
return"Result[status="+this.status+",result="+this.result
+"]";
}
}
publicclassResultDetail{
Locationlocation;
Stringformatted_address;
;
Stringbusiness;
StringcityCode;
publicResultDetail(){
super();
//TODOAuto-generatedconstructorstub
}
publicResultDetail(Locationlocation,Stringformatted_address,
,Stringbusiness,StringcityCode){
super();
this.location=location;
this.formatted_address=formatted_address;
this.addressComponent=addressComponent;
this.business=business;
this.cityCode=cityCode;
}
(){
returnthis.addressComponent;
}
publicStringgetBusiness(){
returnthis.business;
}
publicStringgetCityCode(){
returnthis.cityCode;
}
publicStringgetFormatted_address(){
returnthis.formatted_address;
}
publicLocationgetLocation(){
returnthis.location;
}
publicvoidsetAddressComponent(){
this.addressComponent=addressComponent;
}
publicvoidsetBusiness(Stringbusiness){
this.business=business;
}
publicvoidsetCityCode(StringcityCode){
this.cityCode=cityCode;
}
publicvoidsetFormatted_address(Stringformatted_address){
this.formatted_address=formatted_address;
}
publicvoidsetLocation(Locationlocation){
this.location=location;
}
}
publicclassLocation{
Stringlng;
Stringlat;
publicLocation(){
}
publicLocation(Stringlng,Stringlat){
this.lng=lng;
this.lat=lat;
}
publicStringgetLat(){
returnthis.lat;
}
publicStringgetLng(){
returnthis.lng;
}
publicvoidsetLat(Stringlat){
this.lat=lat;
}
publicvoidsetLng(Stringlng){
this.lng=lng;
}
@Override
publicStringtoString(){
return"Location[lng="+this.lng+",lat="+this.lat+"]";
}
}
publicclassAddressComponent{
Stringcity;
Stringdistrict;
Stringprovince;
Stringstreet;
Stringstreet_number;
publicAddressComponent(){
super();
//TODOAuto-generatedconstructorstub
}
publicAddressComponent(Stringcity,Stringdistrict,Stringprovince,
Stringstreet,Stringstreet_number){
super();
this.city=city;
this.district=district;
this.province=province;
this.street=street;
this.street_number=street_number;
}
publicStringgetCity(){
returnthis.city;
}
publicStringgetDistrict(){
returnthis.district;
}
publicStringgetProvince(){
returnthis.province;
}
publicStringgetStreet(){
returnthis.street;
}
publicStringgetStreet_number(){
returnthis.street_number;
}
publicvoidsetCity(Stringcity){
this.city=city;
}
publicvoidsetDistrict(Stringdistrict){
this.district=district;
}
publicvoidsetProvince(Stringprovince){
this.province=province;
}
publicvoidsetStreet(Stringstreet){
this.street=street;
}
publicvoidsetStreet_number(Stringstreet_number){
this.street_number=street_number;
}
@Override
publicStringtoString(){
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);
Gsongson=newGson();
ResultfromJson=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如何定义.剩下的就非常简单了.

阅读全文

与jasonjava相关的资料

热点内容
pubg用什么服务器 浏览:526
田汉pdf 浏览:661
记录仪如何安装安卓系统 浏览:594
python求灰度均值 浏览:756
c编译器是系统软件吗 浏览:694
获取服务器内网地址 浏览:536
新手妈妈如何带新生儿APP 浏览:157
java日程管理 浏览:376
高清视频链接加密 浏览:407
新买的阿里云服务器怎么配置 浏览:612
在线编译器为什么刷新还在 浏览:212
云服务器系统盘可以装数据库 浏览:906
php绘制图形 浏览:588
支付服务器异常怎么办 浏览:76
java拨号 浏览:868
er5200如何设置虚拟服务器 浏览:571
网络中心服务器叫什么 浏览:459
isplay单片机下载器 浏览:482
怎么查看服务器地址和端口 浏览:187
加密朋克ai 浏览:159