json資料解析,json資料轉為java物件
阿新 • • 發佈:2019-01-29
在Android開發過程中,經常需要與後臺進行資料的互動,JSON作為一種輕量級的資料格式,經常被
2.將json轉換為java物件:首先根據json寫出對應的實體類:
轉換:
使用FastJson進行轉換:
使用GSON進行轉換:
後臺作為傳輸資料的格式,將資料傳輸到客戶端。JSON有兩種格式,一種是物件格式的,另一種是陣列格式的。
下面是一組json字串:
String json="{"resultcode":"200","reason":"Return Successd!","result":{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"聯通","card":""},"error_code":0}"
1.我們按json原生解析的方法一步一步進行解析:
JSONObject jsonObject = new JSONObject(json); JSONObject object = jsonObject.getJSONObject("result"); String province=object.getString("province"); String city=object.getString("city"); String areacode=object.getString("areacode");
2.將json轉換為java物件:首先根據json寫出對應的實體類:
public class Root<T> { private String resultcode; private String reason; private T result; private int error_code; public void setResultcode(String resultcode){ this.resultcode = resultcode; } public String getResultcode(){ return this.resultcode; } public void setReason(String reason){ this.reason = reason; } public String getReason(){ return this.reason; } public void setResult(T result){ this.result = result; } public T getResult(){ return this.result; } public void setError_code(int error_code){ this.error_code = error_code; } public int getError_code(){ return this.error_code; } }
public class result { private String province; private String city; private String areacode; private String zip; private String company; private String card; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } public String getAreacode() { return areacode; } public void setAreacode(String areacode) { this.areacode = areacode; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getCard() { return card; } public void setCard(String card) { this.card = card; } }
轉換:
JSONObject jsonObject=JSONObject.fromObject(json);
Root root=(Root)JSONObject.toBean(jsonObject, Root.class);
使用FastJson進行轉換:
Root<JSON> root=JSONObject.parseObject(json,Root.class);
Result result =JSONObject.toJavaObject(root.getResult(),Result.class);
使用GSON進行轉換:對於有泛型引入的,需要多寫一句話用於獲取泛型資訊。
Gson gson=new Gson();
Type userType = new TypeToken<Root<Result>>(){}.getType();//用於獲取泛型資訊
Root<Result> root=gson.fromJson(json, userType);
Result result=root.getResult();
然後,下面一段json字串中有陣列內容:String json="{"resultcode":"200","reason":"Return Successd!","result":[{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"聯通","card":""},{"province":"北京","city":"","areacode":"010",
"zip":"100000","company":"聯通","card":""}],"error_code":0}"
使用GSON進行轉換:
Gson gson=new Gson();
Type userType = new TypeToken<Root<List<Result>>>(){}.getType();
Root<List<Result>> root=gson.fromJson(json, userType);
List<Result> result=root.getResult();
使用FastJson進行轉換: Root<JSON> root=JSONObject.parseObject(json,Root.class);
List<Result> result =JSONObject.parseArray(root.getResult().toJSONString(),Result.class);
將json陣列轉換為list物件:
String str="[{"name":"真實伺服器","ip":"101.37.168.121","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":false},
{"name":"模擬伺服器","ip":"101.37.34.221","port":5672,"username":"shanghu","password":"GY5P20u1ix9vK8DI","isdemo":true}]";
使用GSON進行轉換: Gson gson=new Gson();
Service[] array = gson.fromJson(str, Service[].class);
List<Service> service=Arrays.asList(array);
使用FastJson進行轉換:
List<Service> service=JSONArray.parseArray(str, Service.class);