首页 > 代码库 > JSON解析

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);

  

 

JSON解析