上篇踩坑了Java解析json字符串直接解析的方式,這篇踩一下解析成javabean的方式的坑兒~還是分三種方式來,先上jsonlib的解析方式~
一、JsonLib
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class jsonLibToJavaBean {
public static void main(String[] args) {
//先來個最簡單的json栗子
String json = "{'continent': '亞洲','country': '中國'}"; //需要建立對應entity
JSONObject jsonObject = JSONObject.fromObject(json);
Continent ci = (Continent) JSONObject.toBean(jsonObject, Continent.class);
System.out.println("continent:" + ci.getContinent() + "," + "country:" + ci.getCountry());
輸出:continent:亞洲,country:中國
System.out.println("====================");
//json數組
String json = "[{'continent': '亞洲','country': '中國'},{'continent': '亞洲','country': '中國'}]";
JSONArray jsonArray = JSONArray.fromObject(json);
List<Object> list = JSONArray.fromObject(jsonArray);
for (int i = 0; i < list.size(); i++) {
JSONObject jsonObject = JSONObject.fromObject(list.get(i));
Continent ci = (Continent) JSONObject.toBean(jsonObject, Continent.class);
System.out.println("continent:" + ci.getContinent() + "," + "country:" + ci.getCountry());
}
/*
* 控制檯輸出
* continent:亞洲,country:中國
* continent:亞洲,country:中國
*/
}
}
//實體類
public class Continent {
private String continent;
private String country;
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
import java.util.List;
import entity.NationalSchool;
import entity.Province;
import net.sf.json.JSONObject;
public class jsonLibToJavaBean {
public static void main(String[] args) {
//下面來個複雜點的栗子,json中帶複雜數據的,例如list、map等
String json = "{'country': '中國','province': [{'city': '西安','postoffice': '012345'},{'city': '洛陽','postoffice': '012345'}]}";
JSONObject jsonObject = JSONObject.fromObject(json);
NationalSchool ns = (NationalSchool) JSONObject.toBean(jsonObject, NationalSchool.class);
String country = ns.getCountry();
System.out.println(country);
List<Province> list = ns.getProvince();
for (int i = 0; i < list.size(); i++) {
JSONObject jb = JSONObject.fromObject(list.get(i));
Province p = (Province) JSONObject.toBean(jb, Province.class);
System.out.println("city:" + p.getCity() + "," + "postoffice:" + p.getPostoffice());
}
//令一種寫法
Map map = new HashMap();
map.put("province", Province.class);
JSONObject jsonObject = JSONObject.fromObject(json);
NationalSchool ns = (NationalSchool) JSONObject.toBean(jsonObject, NationalSchool.class, map);
String country = ns.getCountry();
System.out.println(country);
List<Province> list = ns.getProvince();
for (int i = 0; i < list.size(); i++) {
System.out.println("city:" + list.get(i).getCity() + "," + "postoffice:" + list.get(i).getPostoffice());
}
/*
* 兩種寫法的控制檯輸出
* 中國
* city:西安,postoffice:012345
* city:洛陽,postoffice:012345
*/
/*
* 下面的這種寫法可以不用像上面那樣進行兩次轉換javabean
* 對於剛接觸json轉對象這方面的學生可能會有那麼一點繞
* 這樣可以規避一個錯誤,如果直接這樣寫的話:
* NationalSchool ns = (NationalSchool) JSONObject.toBean(jsonObject, NationalSchool.class);
* List<Province> list = ns.getProvince();
* 直接輸出list是可以的,控制檯輸出
* [net.sf.ezmorph.bean.MorphDynaBean@161cd475[
* {postoffice=012345, city=西安}
* ], net.sf.ezmorph.bean.MorphDynaBean@532760d8[
* {postoffice=012345, city=洛陽}
* ]]
*
* 但是遍歷list時會報錯:
* net.sf.ezmorph.bean.MorphDynaBean cannot be cast to xxx
*/
}
}
//對應實體類
public class NationalSchool implements Serializable {
private static final long serialVersionUID = 1L;
private String country;
private List<Province> province;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public List<Province> getProvince() {
return province;
}
public void setProvince(List<Province> province) {
this.province = province;
}
}
public class Province implements Serializable {
private static final long serialVersionUID = 1L;
private String city;
private String postoffice;
private boolean capital;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostoffice() {
return postoffice;
}
public void setPostoffice(String postoffice) {
this.postoffice = postoffice;
}
public boolean isCapital() {
return capital;
}
public void setCapital(boolean capital) {
this.capital = capital;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
二、FastJson:親身體驗在轉複雜json字符串的時候比JsonLib好用,實體類就不貼了都在上面了,直接貼邏輯代碼
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import entity.Continent;
import entity.NationalSchool;
import entity.Province;
public class fastJsonToJavaBean {
public static void main(String[] args) {
//字符串
String json = "{'continent': '亞洲','country': '中國'}";
JSONObject jsonObject = JSONObject.parseObject(json);
Continent ci = JSONObject.toJavaObject(jsonObject, Continent.class);
System.out.println("continent:" + ci.getContinent() + "," + "country:" + ci.getCountry());
System.out.println("==============================");
//json數組
String jsons = "[{'continent': '亞洲','country': '中國'},{'continent': '亞洲','country': '中國'}]";
JSONArray jsonArray = JSONArray.parseArray(jsons);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObjects = JSONObject.parseObject(jsonArray.get(i).toString());
Continent cis = JSONObject.toJavaObject(jsonObjects, Continent.class);
System.out.println("continent:" + cis.getContinent() + "," + "country:" + cis.getCountry());
}
System.out.println("==============================");
//複雜json
String jsonss = "{'country': '中國','province': [{'city': '西安','postoffice': '012345'},{'city': '洛陽','postoffice': '012345'}]}";
JSONObject jsonObjectss = JSONObject.parseObject(jsonss);
NationalSchool ns = JSONObject.toJavaObject(jsonObjectss, NationalSchool.class);
String country = ns.getCountry();
System.out.println("country:" + country);
List<Province> list = ns.getProvince();
for (int j = 0; j < list.size(); j++) {
System.out.println("city:" + list.get(j).getCity() + "," + "postoffice:" + list.get(j).getPostoffice());
}
System.out.println("==============================");
/*
* FastJson和JsonLib的不同之處就在這裏了
* 這裏對於province可以直接轉成list,而JsonLib不事先把Province對象放到map裏
* 會直接報錯,或者就是像JsonLib中第一種方法分層解析複雜json字符串
* 推薦使用FastJson
*/
/*
* 控制檯輸出
* continent:亞洲,country:中國
* ==============================
* continent:亞洲,country:中國
* continent:亞洲,country:中國
* ==============================
* country:中國
* city:西安,postoffice:012345
* city:西安,postoffice:012345
*/
//來個json數組練練手
String jsonsss = "[{'country': '中國','province': [{'city': '西安','postoffice': '012345'},{'city': '洛陽','postoffice': '012345'}]},{'country': '中國','province': [{'city': '西安','postoffice': '012345'},{'city': '洛陽','postoffice': '012345'}]}]";
JSONArray jsonArrays = JSONArray.parseArray(jsonsss);
for (int k = 0; k < jsonArrays.size(); k++) {
JSONObject jsonObjectsss = JSONObject.parseObject(jsonArrays.get(k).toString());
NationalSchool nss = JSONObject.toJavaObject(jsonObjectsss, NationalSchool.class);
String countrys = ns.getCountry();
System.out.println("country:" + countrys);
List<Province> lists = nss.getProvince();
for (int j = 0; j < lists.size(); j++) {
System.out.println("city:" + lists.get(j).getCity() + "," + "postoffice:" + lists.get(j).getPostoffice());
}
}
}
}
三、Gson
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import entity.Continent;
import entity.NationalSchool;
import entity.Province;
public class gsonToJavaBean {
public static void main(String[] args) {
String json = "{'continent': '亞洲','country': '中國'}";
Gson gson = new Gson();
Continent ci = gson.fromJson(json, Continent.class);
System.out.println("continent:" + ci.getContinent() + "," + "country:" + ci.getCountry());
System.out.println("==============================");
String jsons = "[{'continent': '亞洲','country': '中國'},{'continent': '亞洲','country': '中國'}]";
Gson gsons = new Gson();
List<Continent> list = (List<Continent>) gsons.fromJson(jsons, new TypeToken<List<Continent>>(){}.getType());
for (int i = 0; i < list.size(); i++) {
System.out.println("continent:" + list.get(i).getContinent() + "," + "country:" + list.get(i).getCountry());
}
System.out.println("另一種方法");
JsonArray jsonArray = new JsonParser().parse(jsons).getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonArray.get(i).toString());
System.out.println("continent:" + jsonObject.get("continent").getAsString() + "," + "country:" + jsonObject.get("country").getAsString());
}
System.out.println("==============================");
String jsonss = "{'country': '中國','province': [{'city': '西安','postoffice': '012345'},{'city': '洛陽','postoffice': '012345'}]}";
Gson gsonss = new Gson();
NationalSchool ns = gsonss.fromJson(jsonss, NationalSchool.class);
System.out.println(ns.getCountry());
List<Province> listp = ns.getProvince();
for (int j = 0; j < listp.size(); j++) {
System.out.println("city:" + listp.get(j).getCity() + "," + "postoffice:" + listp.get(j).getPostoffice());
}
/*
* 控制檯輸出
* continent:亞洲,country:中國
* ==============================
* continent:亞洲,country:中國
* continent:亞洲,country:中國
* 另一種方法
* continent:亞洲,country:中國
* continent:亞洲,country:中國
* ==============================
* 中國
* city:西安,postoffice:012345
* city:西安,postoffice:012345
*/
}
}
未完待續...