java的JsonObject对象提取值方法
实例如下所示:
importcom.alibaba.fastjson.JSON; importcom.alibaba.fastjson.JSONArray; importcom.alibaba.fastjson.JSONObject; importio.swagger.models.auth.In; importjava.io.IOException; importjava.security.KeyManagementException; importjava.security.NoSuchAlgorithmException; importjava.util.Iterator; /** *CreatedbyAdministratoron2017/4/21. */ publicclasstest{ publicstaticvoidmain(String[]args)throwsException{ //第一种格式 /** *[ { "data":[ { "building_id":"***", "building_num":"**", "door_name":"**", "electric":"**", "room_name":"**" } ], "success":true } ] */ Strings="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]"; Stringb=s.substring(0,s.length()-1); Stringc=b.substring(1,b.length()); System.out.println(b+"b___"); JSONObjectjsonx=JSON.parseObject(c); JSONArrayja=jsonx.getJSONArray("data"); for(inti=0;i>>>>"); } //第二种格式 /** *[ { "password":"*1234567890", "success":"true" } ] */ Strings="[{\"success\":\"true\",\"password\":\"*1234567890\"}]"; Stringb=s.substring(0,s.length()-1); Stringc=b.substring(1,b.length()); System.out.println(c+"c___"); JSONObjectreagobj=JSONObject.fromObject(c); Stringname=reagobj.getString("password"); System.out.println(name+"name,,,,,,"); Stringpassword=jm.getString("password"); System.out.println(password); System.out.println("看看有没有值"+password); //第三种格式 /** *{ "data":{ "access_token":"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206", "expires_second":36000 }, "rlt_code":"HH0000", "rlt_msg":"成功" } */ Stringres="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}"; JSONObjectjsonObject=JSON.parseObject(res); Stringdata=jsonObject.getString("data"); JSONObjectjsondata=JSON.parseObject(data); Stringtoken=jsondata.getString("access_token"); //第四种格式 /** *{ "data": { "total":23, "start":0, "total_page":3, "rows": [ {"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":"成功" } */ Stringres="{\"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\":\"成功\"}"; JSONObjectjsonObject=JSON.parseObject(res); Stringdata=jsonObject.getString("data"); JSONObjectjsonObjects=JSON.parseObject(data); JSONArrayja=jsonObjects.getJSONArray("rows"); for(inti=0;i 以上这篇java的JsonObject对象提取值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。