當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 編程語言 » javajsontostring

javajsontostring-九游会j9娱乐平台

發布時間: 2024-07-10 17:31:32

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如何定義.剩下的就非常簡單了.

② java實體類怎麼轉換成json。

導入google的包gson-2.2.4.jar
然後實例化gson
static gson gosn = new gson();
string json = gosn.tojson(hashmap); //這里放一個對象,什麼對象都可以。
轉化後就是json,功能強大很多,也簡單很多。

json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
轉換的話這樣用
string s= jsonarray.fromobject(user).tostring();

spring-webmvc4
在方法上加入@responsebody,同時方法返回值為實體對象,spring會自動將對象轉換為json格式,並返回到客戶端

③ java如何返回json格式

例如:
student st1 = new student(1, "dg", 18, new date());
student st2 = new student(2, "dg", 18, new date());
student st3 = new student(3, "dg", 18, new date());
student st4 = new student(4, "dg", 18, new date());
student st5 = new student(5, "dg", 18, new date());
list li = new arraylist();
jsonobject jo1 = new jsonobject(st1);
jsonobject jo2 = new jsonobject(st2);
jsonobject jo3 = new jsonobject(st3);
jsonobject jo4 = new jsonobject(st4);
jsonobject jo5 = new jsonobject(st5);
li.add(jo1);
li.add(jo2);
li.add(jo3);
li.add(jo4);
li.add(jo5);
jsonarray ja = new jsonarray(li);
map ma = new hashmap();
ma.put("result", "ok");
ma.put("records", ja);
jsonobject js = new jsonobject(ma);
out.print(js);

返回結果:

{"result":"ok","records":[{"recorddate":"fri dec 16 17:54:39 cst 2011","name":"dg","age":18,"personid":1},{"recorddate":"fri dec 16 17:54:39 cst 2011","name":"dg","age":18,"personid":2},{"recorddate":"fri dec 16 17:54:39 cst 2011","name":"dg","age":18,"personid":3},{"recorddate":"fri dec 16 17:54:39 cst 2011","name":"dg","age":18,"personid":4},{"recorddate":"fri dec 16 17:54:39 cst 2011","name":"dg","age":18,"personid":5}]}

④ java中如何讀取json文件,在本地有e:/a.json文件,想讀取這個json文件裡面的內容,怎樣實現

//savejsonfile("e:\\yindd\\岩模slaughter.json");
//json文件存放路徑(如:e:\a.json)
string data= readfile.readfile("f:\\a.json");
system.out.println(data);
jsonobject jsonobj = jsonobject.fromobject(data);
//得到a對粗並緩象
jsonarray arraya=jsonobj.getjsonarray("a");
a a = (a) jsonobject.tobean((jsonarray.fromobject(arraya.tostring()).getjsonobject(0)),a.class);
//得到b集合蔽歷
jsonarray arrayb=jsonobj.getjsonarray("b");
list listb=new arraylist();
for(int i=0;ib b=(b)jsonobject.tobean((jsonarray.fromobject(arrayb.tostring()).getjsonobject(i)),b.class);
listb.add(b);
}
//得到c集合
jsonarray arrayc=jsonobj.getjsonarray("c");
list listc=new arraylist();
for(int i=0;ic c=(c)jsonobject.tobean((jsonarray.fromobject(arrayc.tostring()).getjsonobject(i)),c.class);
listb.add(c);
}

⑤ java:arraylist如何轉換為json字元串呢

下一個json的jar包,然後再代碼中使用jsonarray jsonarray = jsonarray.fromobject(/*你的list*/);這樣生成的jsonarray就是一個json字元串。是不是超簡便呢。

⑥ java中怎麼把數據轉換成json數據

搜json-lib.jar
這個包的例子:
jsonobject obj = new jsonobject();
obj.put("name", "kotomi");
obj.tostring();
得到:{"name":"kotomi"}
也可以吧自己定義的實體轉,如
jsonobject.fromobject(xxx);
xxx是你自己定義的實體,他會吧xxx里提供了getter的都轉成json

熱點內容
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:690
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:374
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:954
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:129
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:387
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
androidapk結構 發布:2024-07-17 15:10:43 瀏覽:945
c語言指針的例子 發布:2024-07-17 15:08:01 瀏覽:768
linuxzcat 發布:2024-07-17 15:02:09 瀏覽:901
賓士編程嗎 發布:2024-07-17 14:57:08 瀏覽:853
网站地图