1. json值格式

•数字(整数或浮点数)

•字符串(在双引号中)

•逻辑值(true或 false)

•null

•数组(在方括号中) 例如:{“Teachers”:[“roy”,”will”]}

String arrayJson="[1,3,4]";//转换成json数组(用JSON.paraseArray)JSONArray arrayJO=JSON.parseArray(arrayJson);//jsonArray通过下标获取存储的第二个元素System.out.println("存储的第二个元素是"+arrayJO.get(1));

#输出结果

存储的第二个元素是3

•对象(在花括号中) 例如:{“course”:{“teacher”:”roy”,”courese”:”java”}}

#例1: jsonString值

String jsonString="{\"status\":200,\"msg\":\"查询成功\",\"nickname\":\"测试账号\",\"id\":\"12831\",\"describe\":\"will的测试账号\"}";//转换成json 对象(用JSON.paraseObject)JSONObject jsonObject=JSON.parseObject(jsonString);System.out.println(jsonObject);//jsonObject通过键的方式获取值System.out.println("Nickname的值是"+jsonObject.get("nickname"));

#输出结果

# jsonString值输出结果{"msg":"查询成功","nickname":"测试账号","id":"12831","describe":"will的测试账号","status":200}Nickname的值是测试账号

#例2:Object值

//将对象的属性写成 键:值的格式。WashMachine wm=new WashMachine();//*object或其他格式使用toJSONString方法转成JSON格式的字符串String s =JSON.toJSONString(wm);System.out.println(s);//将字符串转成JSONobjectJSONObject jsonObject= JSON.parseObject(s);System.out.println(jsonObject);//获取某个值Object canUse=jsonObject.get("canUse");

#输出结果

#Object值输出结果{"age":10,"brand":"米","canUse":false,"capacity":90,"desc":["美观","大气","有烘干功能"],"dog":{"age":0,"ear":"尖尖的耳朵"},"king":"电器","price":0.0,"weight":40}false
  1. json的使用

#Json的使用

//删jsonObject.remove("brand");System.out.println(jsonObject);//改jsonObject.replace("king","洗衣机");System.out.println(jsonObject);//增jsonObject.put("volume","38");System.out.println(jsonObject);//遍历 json字符串中的不同类型格式所对应的json处理方式//对象object使用keyset()去遍历for (String key : jsonObject.keySet()) {//先获取到Object类型Object value=jsonObject.get(key); if(value instanceof String){System.out.println(value+"是个字符串");//强转数据类型成String//String value1=(String)value; }else if(value instanceof Boolean){System.out.println(value+"是个布尔值"); }else if(value instanceof BigDecimal){System.out.println(value+"是个浮点数"); }else if(value instanceof Integer){System.out.println(value+"是个整数"); }else if(value instanceof JSONObject){System.out.println(value+"是个json对象"); }else if(value instanceof JSONArray){System.out.println(value+"是个json数组");}}

#输出结果

//删{"king":"电器","price":0.0,"weight":40,"dog":{"ear":"尖尖的耳朵","age":0},"age":10,"canUse":false,"capacity":90,"desc":["美观","大气","有烘干功能"]}//改{"king":"洗衣机","price":0.0,"weight":40,"dog":{"ear":"尖尖的耳朵","age":0},"age":10,"canUse":false,"capacity":90,"desc":["美观","大气","有烘干功能"]}//增{"volume":"38","king":"洗衣机","price":0.0,"weight":40,"dog":{"ear":"尖尖的耳朵","age":0},"age":10,"canUse":false,"capacity":90,"desc":["美观","大气","有烘干功能"]}//遍历38是个字符串洗衣机是个字符串0.0是个浮点数40是个整数{"ear":"尖尖的耳朵","age":0}是个json对象10是个整数false是个布尔值90是个整数["美观","大气","有烘干功能"]是个json数组狗对象的耳朵属性是尖尖的耳朵获取desc数组中的第二个值是有烘干功能尖尖的耳朵米是个字符串
  1. Json的解析

方法1:直接解析

 String s="jQuery1102004922028985799365_1676625243870({\"Srcid\":\"5809\",\"ResultCode\":\"0\",\"status\":\"0\",\"QueryID\":\"2479201133\",\"Result\":[{\"DisplayData\":{\"strategy\":{\"tempName\":\"ip\",\"precharge\":\"0\",\"ctplOrPhp\":\"1\"},\"resultData\":{\"tplData\":{\"srcid\":\"5809\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\",\"origipquery\":\"163.228.65.13\",\"query\":\"163.228.65.13\",\"origip\":\"163.228.65.13\",\"location\":\"\\u4e0a\\u6d77\\u5e02 \\u5fae\\u8f6f\\u4e91\",\"userip\":\"\",\"showlamp\":\"1\",\"tplt\":\"ip\",\"titlecont\":\"IP\\u5730\\u5740\\u67e5\\u8be2\",\"realurl\":\"http:\\/\\/www.ip138.com\\/\",\"showLikeShare\":\"1\",\"shareImage\":\"1\",\"data_source\":\"AE\"},\"extData\":{\"tplt\":\"ip\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\"}}},\"ResultURL\":\"http:\\/\\/www.ip138.com\\/\",\"Weight\":\"2\",\"Sort\":\"1\",\"SrcID\":\"5809\",\"ClickNeed\":\"0\",\"SubResult\":[],\"SubResNum\":\"0\",\"ar_passthrough\":[],\"RecoverCacheTime\":\"0\"}],\"data\":[{\"srcid\":\"5809\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\",\"origipquery\":\"163.228.65.13\",\"query\":\"163.228.65.13\",\"origip\":\"163.228.65.13\",\"location\":\"\\u4e0a\\u6d77\\u5e02 \\u5fae\\u8f6f\\u4e91\",\"userip\":\"\",\"showlamp\":\"1\",\"tplt\":\"ip\",\"titlecont\":\"IP\\u5730\\u5740\\u67e5\\u8be2\",\"realurl\":\"http:\\/\\/www.ip138.com\\/\",\"showLikeShare\":\"1\",\"shareImage\":\"1\"}],\"ResultNum\":\"1\"})";//直接解析JSONObject tplDataJson=jsonObject.getJSONArray("Result").getJSONObject(0).getJSONObject("DisplayData").getJSONObject("resultData").getJSONObject("tplData");//用object的keyset去遍历for (String key : tplDataJson.keySet()) {System.out.println(key+"的值是"+tplDataJson.get(key));}

#输出结果

resourceid的值是5809OriginQuery的值是163.228.65.13origipquery的值是163.228.65.13query的值是163.228.65.13shareImage的值是1origip的值是163.228.65.13showlamp的值是1data_source的值是AEtplt的值是iptitlecont的值是IP地址查询srcid的值是5809realurl的值是http://www.ip138.com/userip的值是location的值是上海市 微软云showLikeShare的值是1

方法2:JsonPath解析

$是表示根目录,.(点)是表示下一层级

 String s="jQuery1102004922028985799365_1676625243870({\"Srcid\":\"5809\",\"ResultCode\":\"0\",\"status\":\"0\",\"QueryID\":\"2479201133\",\"Result\":[{\"DisplayData\":{\"strategy\":{\"tempName\":\"ip\",\"precharge\":\"0\",\"ctplOrPhp\":\"1\"},\"resultData\":{\"tplData\":{\"srcid\":\"5809\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\",\"origipquery\":\"163.228.65.13\",\"query\":\"163.228.65.13\",\"origip\":\"163.228.65.13\",\"location\":\"\\u4e0a\\u6d77\\u5e02 \\u5fae\\u8f6f\\u4e91\",\"userip\":\"\",\"showlamp\":\"1\",\"tplt\":\"ip\",\"titlecont\":\"IP\\u5730\\u5740\\u67e5\\u8be2\",\"realurl\":\"http:\\/\\/www.ip138.com\\/\",\"showLikeShare\":\"1\",\"shareImage\":\"1\",\"data_source\":\"AE\"},\"extData\":{\"tplt\":\"ip\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\"}}},\"ResultURL\":\"http:\\/\\/www.ip138.com\\/\",\"Weight\":\"2\",\"Sort\":\"1\",\"SrcID\":\"5809\",\"ClickNeed\":\"0\",\"SubResult\":[],\"SubResNum\":\"0\",\"ar_passthrough\":[],\"RecoverCacheTime\":\"0\"}],\"data\":[{\"srcid\":\"5809\",\"resourceid\":\"5809\",\"OriginQuery\":\"163.228.65.13\",\"origipquery\":\"163.228.65.13\",\"query\":\"163.228.65.13\",\"origip\":\"163.228.65.13\",\"location\":\"\\u4e0a\\u6d77\\u5e02 \\u5fae\\u8f6f\\u4e91\",\"userip\":\"\",\"showlamp\":\"1\",\"tplt\":\"ip\",\"titlecont\":\"IP\\u5730\\u5740\\u67e5\\u8be2\",\"realurl\":\"http:\\/\\/www.ip138.com\\/\",\"showLikeShare\":\"1\",\"shareImage\":\"1\"}],\"ResultNum\":\"1\"})";//用Jsonpath解析,然后用toString转成字符串String tplDataString= JSONPath.read(json, "$.Result.DisplayData.resultData.tplData").toString();System.out.println(tplDataString);

#输出结果

[{"resourceid":"5809","OriginQuery":"163.228.65.13","origipquery":"163.228.65.13","query":"163.228.65.13","shareImage":"1","origip":"163.228.65.13","showlamp":"1","data_source":"AE","tplt":"ip","titlecont":"IP地址查询","srcid":"5809","realurl":"http://www.ip138.com/","userip":"","location":"上海市 微软云","showLikeShare":"1"}]

4.Json与其他类型的转换

Json转成Map

#Json字符串转成MapString s="{resourceid=5809, OriginQuery=163.228.65.13, origipquery=163.228.65.13, query=163.228.65.13, shareImage=1, origip=163.228.65.13, showlamp=1, data_source=AE, tplt=ip, titlecont=IP地址查询, srcid=5809, realurl=http://www.ip138.com/, userip=, location=上海市 微软云, showLikeShare=1}";#方法1//先把字符串转成objectJsonObject tplDataJson=JSONObject.paraseObject(s);//然后通过object.keyset()遍历键值加到map中,就实现了map的转化Map map=new HashMap();for (String key : tplDataJson.keySet()) { map.put(key, tplDataJson.getString(key));}System.out.println(map);#方法2//直接用JSONObject.paraseObject()方法直接转HashMap hashMap= JSONObject.parseObject(s,HashMap.class);System.out.println(hashMap);

#输出结果

#方法1{resourceid=5809, OriginQuery=163.228.65.13, origipquery=163.228.65.13, query=163.228.65.13, shareImage=1, origip=163.228.65.13, showlamp=1, data_source=AE, tplt=ip, titlecont=IP地址查询, srcid=5809, realurl=http://www.ip138.com/, userip=, location=上海市 微软云, showLikeShare=1}#方法2{resourceid=5809, OriginQuery=163.228.65.13, origipquery=163.228.65.13, query=163.228.65.13, shareImage=1, origip=163.228.65.13, showlamp=1, data_source=AE, tplt=ip, titlecont=IP地址查询, srcid=5809, realurl=http://www.ip138.com/, userip=, location=上海市 微软云, showLikeShare=1}

Map转成json

将map拼接为json字符串//注意Json的格式:键一定有双引号,值不一定,值只有字符串的时候才有双引号String reJson="{";//遍历map中的每个键for (String key : map.keySet()) { if(map.get(key) instanceof String) { //值是字符串的时候 //拼接键和值:“键”:"map.get(key)" reJson+=String.format("\"%s\":\"%s\",", key, map.get(key)); }else { //值不是字符串的时候,“键”:map.get(key) reJson+=String.format("\"%s\":%s,", key, map.get(key));}}reJson=reJson.substring(0,reJson.length()-1)+"}";System.out.println(reJson);

#输出结果

{"resourceid":"5809","OriginQuery":"163.228.65.13","origipquery":"163.228.65.13","query":"163.228.65.13","shareImage":"1","origip":"163.228.65.13","showlamp":"1","data_source":"AE","tplt":"ip","titlecont":"IP地址查询","srcid":"5809","realurl":"http://www.ip138.com/","userip":"","location":"上海市 微软云","showLikeShare":"1"}

Json转成其他类型

#Json转成JsonObjectJSONObject jsonObject=JSONObject.parseObject(json字符串);#Json转成字符串String s= (json字符串).toString();#对象可以转成json字符串//将对象的属性写成 键:值的格式。WashMachine wm=new WashMachine();//其他格式转成JSON格式的字符串String s =JSON.toJSONString(wm);