zoukankan      html  css  js  c++  java
  • java解析json数据

    json数据一般分两种,对象和数组。

    json解析需要写异常处理。

    解析json对象用的是JSONObject类,如

        public void analys_jsonObject(String jsonData){//jsonData就是对象格式的json数据
            try {
                JSONObject jsonObject = new JSONObject(jsonData);
                String one_string = jsonObject.getString("键值名");//通过键值名获取字符串
                JSONObject one_jsonObject = jsonObject.getJSONObject("键值名");//通过键值名获取对象格式的json对象
                JSONArray one_jsonArray = jsonObject.getJSONArray("键值名");//通过键值名获取数组格式的json对象
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    JSONArray类的用法类似:

        public void analys_jsonArray(String jsonData){//jsonData就是数组格式的json数据
            try {
                JSONArray jsonArray = new JSONArray(jsonData);
                String one_json = jsonArray.getString(0);//通过int型数字提取数据
                JSONObject one_jsonObject = jsonArray.getJSONObject(0);//通过int型数字获取对象格式的json对象
                JSONArray one_jsonArray = jsonArray.getJSONArray(0);//通过int型数字获取数组格式的json对象
            }catch (Exception e){
                e.printStackTrace();
            }
        }
  • 相关阅读:
    Linux定时任务调度
    Linux组管理和权限管理
    Linux压缩和解压缩类指令
    leetcode:Compare Version Numbers
    leetcode:Valid Palindrome
    Majority Element
    Min Stack
    leetcode:Intersection of Two Linked Lists(两个链表的交叉点)
    leetcode:Factorial Trailing Zeroes
    leetcode:Rotate Array
  • 原文地址:https://www.cnblogs.com/red-code/p/5878067.html
Copyright © 2011-2022 走看看