java的JsonObject对象提取值


[java]   view plain   copy
  1.   
[java]   view plain   copy
  1. import com.alibaba.fastjson.JSON;  
  2. import com.alibaba.fastjson.JSONArray;  
  3. import com.alibaba.fastjson.JSONObject;  
  4. import io.swagger.models.auth.In;  
  5. import java.io.IOException;  
  6. import java.security.KeyManagementException;  
  7. import java.security.NoSuchAlgorithmException;  
  8. import java.util.Iterator;  
  9.   
  10. /** 
  11.  * Created by Administrator on 2017/4/21. 
  12.  */  
  13. public class test {  
  14.   
  15.     public static void main(String[] args) throws Exception {  
  16.          
  17.        //第一种格式  
  18.        /** 
  19.         * [ 
  20.             { 
  21.                 "data ": [ 
  22.                     { 
  23.                         "building_id ": "*** ", 
  24.                         "building_num ": "** ", 
  25.                         "door_name ": "** ", 
  26.                         "electric ": "** ", 
  27.                         "room_name ": "** " 
  28.                     } 
  29.                 ], 
  30.                 "success ": true 
  31.                 } 
  32.             ] 
  33.         */  
  34.         String s="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]" ;  
  35.         String b= s.substring(0,s.length()-1);  
  36.         String c=b.substring(1, b.length());  
  37.         System.out.println(b+"b___");  
  38.         JSONObject jsonx = JSON.parseObject(c);  
  39.         JSONArray ja = jsonx.getJSONArray("data");  
  40.         for (int i = 0; i < ja.size(); i++) {  
  41.             JSONObject jo = ja.getJSONObject(i);  
  42.             String building_id = jo.getString("building_id");  
  43.             System.out.println(building_id+"building_id>>>>>");  
  44.         }  
  45.   
  46.        //第二种格式  
  47.        /** 
  48.          * [ 
  49.                 { 
  50.                     "password ": "*1234567890 ", 
  51.                     "success ": "true " 
  52.                 } 
  53.             ] 
  54.          */  
  55.         String s="[{\"success\":\"true\",\"password\":\"*1234567890\"}]";  
  56.         String b= s.substring(0,s.length()-1);  
  57.         String c=b.substring(1, b.length());  
  58.         System.out.println(c+"c___");  
  59.         JSONObject reagobj = JSONObject.fromObject(c);  
  60.         String name = reagobj.getString("password");  
  61.         System.out.println(name+"name,,,,,,");  
  62.         String password = jm.getString("password");  
  63.         System.out.println(password);  
  64.         System.out.println("看看有没有值"+password);     
  65.   
  66.          
  67.         //第三种格式  
  68.         /** 
  69.         * { 
  70.             "data ": { 
  71.                 "access_token ": "5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206 ", 
  72.                 "expires_second ": 36000 
  73.             }, 
  74.             "rlt_code ": "HH0000 ", 
  75.             "rlt_msg ": "成功 " 
  76.         } 
  77.          */  
  78.         String res="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";  
  79.         JSONObject jsonObject= JSON.parseObject(res);  
  80.         String data = jsonObject.getString("data");  
  81.         JSONObject jsondata= JSON.parseObject(data);  
  82.         String token = jsondata.getString("access_token");  
  83.   
  84.           
  85.         //第四种格式  
  86.          /** 
  87.          * {  
  88.             "data ": 
  89.                 {  
  90.                     "total ":23,  
  91.                     "start ":0,  
  92.                     "total_page ":3,  
  93.                     "rows ": 
  94.                         [ 
  95.                             { "op_way ": "3 ", "user_mobile ": "15321918571 ", "op_time ":1493881391976, "pwd_no ":30}, 
  96.                             { "op_way ": "1 ", "op_time ":1493880995000, "pwd_no ":31} 
  97.                         ],  
  98.                     "current_page ":1,  
  99.                     "page_size ":10 
  100.                 },  
  101.             "rlt_code ": "HH0000 ", 
  102.             "rlt_msg ": "成功 " 
  103.         } 
  104.         */  
  105.         String res="{\"data\":{\"total\":23,\"start\":0,\"total_page\":3,\"rows\":[{\"op_way\":\"1\",\"op_time\":1493884964000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493884615032,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493883836552,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493883294000,\"pwd_no\":31},{\"op_way\":\"1\",\"op_time\":1493883256000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493883015371,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493882007000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881498520,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881391976,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493880995000,\"pwd_no\":31}],\"current_page\":1,\"page_size\":10},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}";  
  106.         JSONObject jsonObject= JSON.parseObject(res);  
  107.         String data = jsonObject.getString("data");  
  108.         JSONObject jsonObjects= JSON.parseObject(data);  
  109.         JSONArray ja = jsonObjects.getJSONArray("rows");  
  110.         for (int i = 0; i < ja.size(); i++) {  
  111.             JSONObject jo = ja.getJSONObject(i);  
  112.             String op_way = jo.getString("op_way");  
  113.             String op_time = jo.getString("op_time");  
  114.             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  115.             long lt = new Long(op_time);  
  116.             Date date = new Date(lt);  
  117.             res = simpleDateFormat.format(date);  
  118.             String pwd_no = jo.getString("pwd_no");  
  119.             String user_mobile = jo.getString("user_mobile");  
  120.             System.out.println(op_way+res+pwd_no+user_mobile+"------------");  
  121.         }  
  122.   
  123.        
  124.       
  125.     }  
  126.   
  127.     }  

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值