導航:首頁 > 編程語言 > 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相關的資料

熱點內容
如何用電腦解壓縮文件 瀏覽:444
pubg用什麼伺服器 瀏覽:526
田漢pdf 瀏覽:661
記錄儀如何安裝安卓系統 瀏覽:594
python求灰度均值 瀏覽:756
c編譯器是系統軟體嗎 瀏覽:694
獲取伺服器內網地址 瀏覽:536
新手媽媽如何帶新生兒APP 瀏覽:157
java日程管理 瀏覽:376
高清視頻鏈接加密 瀏覽:407
新買的阿里雲伺服器怎麼配置 瀏覽:612
在線編譯器為什麼刷新還在 瀏覽:213
雲伺服器系統盤可以裝資料庫 瀏覽:906
php繪制圖形 瀏覽:588
支付伺服器異常怎麼辦 瀏覽:76
java撥號 瀏覽:868
er5200如何設置虛擬伺服器 瀏覽:572
網路中心伺服器叫什麼 瀏覽:459
isplay單片機下載器 瀏覽:482
怎麼查看伺服器地址和埠 瀏覽:187