JSON解析
1.API
Xxx getXxx(int index) : 根據下標得到json數組中對應的元素數據
Xxx optXxx(int index) : 根據下標得到json數組中對應的元素數據
注意:
optXxx方法會在對應的key中的值不存在的時候返回一個空字符串或者返回你指定的默認值,但是getString方法會出現空指針異常的錯誤。
2.特殊json數據解析
{ "code": 0, "list": { "0": { "aid": "6008965", "author": "嗶哩嗶哩番劇", "coins": 170, "copyright": "Copy", "create": "2016-08-25 21:34" }, "1": { "aid": "6008938", "author": "嗶哩嗶哩番劇", "coins": 404, "copyright": "Copy", "create": "2016-08-25 21:33" } } }
public class FilmInfo {
private int code;
private List<FilmBean> list;
public static class FilmBean{
private String aid;
private String author;
private int coins;
private String copyright;
private String create;
}
}
// 創建封裝的Java對象
FilmInfo filmInfo = new FilmInfo();
// 2 解析json
try {
JSONObject jsonObject = new JSONObject(json);
// 第一層解析
int code = jsonObject.optInt("code");
JSONObject list = jsonObject.optJSONObject("list");
// 第一層封裝
filmInfo.setCode(code);
List<FilmInfo.FilmBean> lists = new ArrayList<>();
filmInfo.setList(lists);
// 第二層解析
for (int i = 0; i < list.length(); i++) {
JSONObject jsonObject1 = list.optJSONObject(i + "");
if(jsonObject1 != null) {
String aid = jsonObject1.optString("aid");
String author = jsonObject1.optString("author");
int coins = jsonObject1.optInt("coins");
String copyright = jsonObject1.optString("copyright");
String create = jsonObject1.optString("create");
// 第二層數據封裝
FilmInfo.FilmBean filmBean = new FilmInfo.FilmBean();
filmBean.setAid(aid);
filmBean.setAuthor(author);
filmBean.setCoins(coins);
filmBean.setCopyright(copyright);
filmBean.setCreate(create);
lists.add(filmBean);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
3.Gson框架技術
(1)將json格式的字符串{}轉換為Java對象
API:
fromJson(String json, Class<T> classOfT);
步驟 1)將Gson的jar包導入到項目中 2)創建Gson對象 : Gson gson = new Gson(); 3)通過創建的Gson對象調用fromJson()方法,返回該JSON數據對應的Java對象: ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
Gson gson = new Gson(); ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
(2)將json格式的字符串[]轉換為Java對象的List
API:
fromJson(String json, Type typeOfT);
Gson gson = new Gson();
List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() {}.getType());
(3)將Java對象轉換為json字符串{}
API:
String toJson(Object src);
// 1 獲取或創建Java對象 ShopInfo shopInfo = new ShopInfo(1,"鮑魚",250.0,"baoyu"); // 2 生成JSON數據 Gson gson = new Gson(); String json = gson.toJson(shopInfo);
(4)將Java對象的List轉換為json字符串[]
API:
String toJson(Object src);
// 1 獲取或創建Java對象 List<ShopInfo> shops = new ArrayList<>(); ShopInfo baoyu = new ShopInfo(1, "鮑魚", 250.0, "baoyu"); ShopInfo longxia = new ShopInfo(2, "龍蝦", 251.0, "longxia"); shops.add(baoyu); shops.add(longxia); // 2 生成JSON數據 Gson gson = new Gson(); String json = gson.toJson(shops);
4.FastJson框架技術
(1)將json格式的字符串{}轉換為Java對象
API:
parseObject(String json, Class<T> classOfT);
ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);
(2)將json格式的字符串[]轉換為Java對象的List
API:
List<T> parseArray(String json,Class<T> classOfT);
List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
(3)將Java對象轉換為json字符串{}
API:
String toJSONString(Object object);
步驟:
1)導入fastjson的jar包
2)JSON調用toJSONString()方法,獲取轉換后的json數據
例如:
ShopInfo shopInfo = new ShopInfo(1, "鮑魚", 250.0, "baoyu"); String json = JSON.toJSONString(shopInfo);
(4)將Java對象的List轉換為json字符串[]
API:
String toJSONString(Object object);
List<ShopInfo> shops = new ArrayList<>(); ShopInfo baoyu = new ShopInfo(1, "鮑魚", 250.0, "baoyu"); ShopInfo longxia = new ShopInfo(2, "龍蝦", 251.0, "longxia"); shops.add(baoyu); shops.add(longxia); String json = JSON.toJSONString(shops);
浙公網安備 33010602011771號