JSON解析,json線上解析
1.API
Xxx getXxx(int index) : 根據下標得到json數組中對應的元素資料
Xxx optXxx(int index) : 根據下標得到json數組中對應的元素資料
注意:
optXxx方法會在對應的key中的值不存在的時候返回一個Null 字元串或者返回你指定的預設值,但是getString方法會出現null 指標異常的錯誤。
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 解析jsontry {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);