美文网首页Android开发
JSON.parseObject的几种用法

JSON.parseObject的几种用法

作者: 皓皓amous | 来源:发表于2019-08-06 14:07 被阅读0次

    以下只是草稿,较粗糙,详细的用法请参考:

    FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

    一.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")
    

    三.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

    1.1 JSONobject=>javaBean

    JSONObject contentChild = contentsArray.getJSONObject(i);
    QCCustomerScore.CustomerCore customerCore = JSON.toJavaObject(contentChild, QCCustomerScore.CustomerCore.class);
    

    1.2 String转javaBean

    见:FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

    相关文章

      网友评论

        本文标题:JSON.parseObject的几种用法

        本文链接:https://www.haomeiwen.com/subject/dniidctx.html