zoukankan      html  css  js  c++  java
  • java解析多级Json中的数组

    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class Test {
    
        public static void main(String[] args) throws Exception{
            //原始String类型json
            String aaa = "{"code":"","errorCode":"200","errorContext":null,"message":"\r\n","resultObject":
            {"listDataMap":[{"successRuleCount":22,"failRuleCount":0,"checkSuccess":"true","checkUrl":"http://www.baidu.com"},{}]},"success":true}";
    
             //先转成JSONObject,取出第一层resultObject中的内容
            JSONObject resultObject =  (JSONObject)getValue(new JSONObject(aaa),"resultObject");
    
          //这里因为listDataMap中是数组的形式,所以要转成JSONArray来取
            JSONArray listDataMap =  (JSONArray)getValue(resultObject,"listDataMap");
    
            //取出数组中第一个值里面所需要的key对应的value
            System.out.println(getValue(listDataMap.getJSONObject(0),"checkSuccess"));
            System.out.println(getValue(listDataMap.getJSONObject(0),"checkUrl"));
    
        }
    
        public static Object getValue(JSONObject json, String key){
            if(json ==null){
                return null;
            }else {
                if(json.isNull(key)){
                    return null;
                }else {
                    return json.get(key);
                }
            }
        }
    }

     方法二

    解析普通json

    result
    {
        "success":"true",
        "returnAddress":"123"
    }
    
    
       JSONObject jsonObject=JSON.parseObject(result);      //转换成object
     
       jsonObject.getString("returnAddress")    //获取object中returnAddress字段;   

    result
    {
        "success":"true",
        "data":{
            "shop_uid":"123"
        }
    }
     
    
    JSONObject shop_user =JSON.parseObject(result);
     
    JSON.parseObject(shop_user.getString("data")).getString("shop_uid")

    解析数组型json

    result
    {
        "success":"true",
        "data":[{
            "shop_uid":"123"
        },
        {
            "shop_name":"张三"
        }]
    }
     
    
    JSONArray detail = JSON.parseArray(result);
    for (int i=0; i<detail.size();i++){
        if(detail.get(i)!=null||!detail.get(i).equals("")){
            JSONArray detailChild =detail.getJSONArray(i);
            if(detailChild.getInteger(1)>Integer.valueOf(ship.get("shiptime").toString())){
                ship.put("shiptime",detailChild.getInteger(1));
                ship.put("desc",detailChild.getString(0));
            }
        }
    }

    JSON转javaBean

    JSONobject=>javaBean
    
    JSONObject contentChild = contentsArray.getJSONObject(i);
    QCCustomerScore.CustomerCore customerCore = JSON
            .toJavaObject(contentChild, QCCustomerScore.CustomerCore.class);
  • 相关阅读:
    《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS)
    [xdoj]1303jlz的刷题黑科技
    [xdoj]1299&1300朱神的烦恼 朱神的序列
    [洛谷]p1996约瑟夫环 &xdoj1311
    [xdoj] 1310 DSKer的卡牌游戏
    [xdoj] 1301&1302 数字计数 数字计数的复仇
    随手快递app开发第十六天
    随手快递app开发第十五天
    随手快递app开发第十四天
    随手快递app开发第十三天
  • 原文地址:https://www.cnblogs.com/hkgov/p/13051396.html
Copyright © 2011-2022 走看看