JSON的各种解析小结

JSON的各种解析

JSON是对象类型解析

  • 数据是 {“name”:”zhangsanfeng”,”age”:3,”sex”:”nv”}
  • protected User readJsonObject(String jsonData) {
    
        // 内置解析方法JSONObject
        User bean = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
    
            String xname = jsonObject.getString("name");
            int xage = jsonObject.getInt("age");
            String xsex = jsonObject.getString("sex");
    
            bean = new User(xname, xage, xsex);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }
    

JSON是数组类型解析

  • 数据是
  •  [
    {"name":"zhangsanfeng","age":3,"sex":"nv"},
    {"name":"zhaobenshan","age":2,"sex":"renyao"}
    ]
    
    protected List<User> readJsonArray(String jsonData) {
            // 内置解析方法JSONObject
            List<User> users = new ArrayList<User>();
            try {
                JSONArray jsonArray = new JSONArray(jsonData);
    
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
    
                    String xname = jsonObject.getString("name");
                    int xage = jsonObject.getInt("age");
                    String xsex = jsonObject.getString("sex");
    
                    User user = new User(xname, xage, xsex);
    
                    users.add(user);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return users;
        }
    

JSON是综合类型解析(对象中包含数组,数组中包含对象)

  • 数据是

    {
        "error_code": 0,
        "reason": "查询成功!",
        "result": {
            "future": [
                {
                    "date": "20140804",
                    "temperature": "28℃~36℃",
                    "weather": "晴转多云",
                    "weather_id": {
                        "fa": "00",
                        "fb": "01"
                    },
                    "week": "星期一",
                    "wind": "南风3-4级"
                },
                {
                    "date": "20140805",
                    "temperature": "28℃~36℃",
                    "weather": "晴转多云",
                    "weather_id": {
                        "fa": "00",
                        "fb": "01"
                    },
                    "week": "星期二",
                    "wind": "东南风3-4级"
                },
                {
                    "date": "20140806",
                    "temperature": "27℃~35℃",
                    "weather": "晴转多云",
                    "weather_id": {
                        "fa": "00",
                        "fb": "01"
                    },
                    "week": "星期三",
                    "wind": "东南风3-4级"
                },
                {
                    "date": "20140807",
                    "temperature": "27℃~34℃",
                    "weather": "多云",
                    "weather_id": {
                        "fa": "01",
                        "fb": "01"
                    },
                    "week": "星期四",
                    "wind": "东南风3-4级"
                },
        "resultcode": "200"
    }
    
    
    protected List<Future> readJsonObjArr(String jsonData) {
            // 内置解析方法JSONObject
            List<Future> futures = new ArrayList<Future>();
            try {
                JSONObject jsonObject = new JSONObject(jsonData);
    
                JSONObject result = jsonObject.getJSONObject("result");
    
                JSONArray jsonArray = result.getJSONArray("future");
    
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject future = jsonArray.getJSONObject(i);
                    String xdate = future.getString("date");
                    String xtemperature = future.getString("temperature");
                    String xweather = future.getString("weather");
                    String xweek = future.getString("week");
                    String xwind = future.getString("wind");
    
                    Future f = new Future(xdate, xtemperature, xweather, xweek, xwind);
    
                    futures.add(f);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return futures;
        }
    

用框架对JSON解析

  1. 对象

    protected User readJsonObject(String jsonData) {
        // 内置解析方法JSONObject,ctrl+k
        User bean = null;
        bean = JsonUtil.parseJsonToBean(jsonData, User.class);
        return bean;
    }
    
  2. 数组

    users = (List<User>) JsonUtil.parseJsonToList(jsonData, new TypeToken<List<User>>(){}.getType());
    
  3. 综合

    protected List<Future> readJsonObjArr(String jsonData) {
        // 内置解析方法JSONObject
        List<Future> futures = new ArrayList<Future>();
    
        String result = JsonUtil.getFieldValue(jsonData, "result");
    
    
        String future = JsonUtil.getFieldValue(result, "future");
    
         JSONArray jsonArray;
        try {
            jsonArray = new JSONArray(future);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
    
                String xdate = jsonObject.getString("date");
                String xtemperature = jsonObject.getString("temperature");
                String xweather = jsonObject.getString("weather");
                String xweek = jsonObject.getString("week");
                String xwind = jsonObject.getString("wind");
    
                Future f = new Future(xdate, xtemperature, xweather, xweek, xwind);
    
                futures.add(f);
    
            }
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    
        return futures;
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值