使用alibaba的jar包
1 简析
2 代码演练
1 简析
1.1 Json 打印
- log.info("aa->bb->cc->:{}",var);
- catch的异常一定要打印堆栈
- JSON.toJSONString(var)
优点:无格式,不用分行,查日志,特别是树状节点好查
缺点:客户端送来的null,打印的时候不打印,Date类型无法打印成指定格式
List<Integer> list4 = List.of(1,2,3,4); System.out.println("list4=="+JSON.toJSONString(list4,true));
- JsonUtils.toJson(var)
优点:打印Date类型可以指定格式
@JsonFormat(pattern = "yyyy-MM--dd",timezone = "GMT+8")
https://www.cnblogs.com/1446358788-qq/p/16573781.html
1.2 Json 插入map
HashMap mapCert = new HashMap();
jJsonDemo.put("vegetable", JSONObject.toJSON(mapCert));
1.3 Json 解析单个对象判断(详细见下方demo)
Object oVegetable instanceof JSONObject
1.4 Json 解析json集合判断(详细见下方demo)
Object Vegetable instanceof JSONArray
1.5 Json 转化为 String
String sJsonDemo = jJsonDemo.toJSONString();
1.6 String 转化为 json
String sJsonDemo = "";
JSONObject jJsonDemo=JSONObject.parseObject(sJsonDemo);
1.7 Json 替换对象
((JSONObject) jJsonDemo.get("fruits")).put("red", "watermelon");
1.8 Json 获取元素
String sss= jJsonDemo.get("vegetable").toString();
2 案例
package com.ddwei.json; import java.util.HashMap; import java.util.StringTokenizer; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class JsonDemo1 { JSONObject jJsonDemo = null; private String sJsonDemo1 = "{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"} ] }}}"; private String sJsonDemo2 = "{\"fruits\":{\"red\":\"apple\",\"yellow\":\"banana\",\"orange\":\"orange\"},\"vegetable\":\"carrot\"}"; private String sJsonDemo3 = "{\"fruits\":[{\"red\":\"apple\"},{\"yellow\":\"banana\"},{\"orange\":\"orange\"}],\"vegetable\":{\"carrot\":\"carrot\"}}"; /** * sJsonDemo1 * "menu":{ * "id":"file", * "popup":{ * "menuitem":[ * {"value":"New","onclick":"CreateNewDoc()"}, * {"value":"Open","onclick":"OpenDoc()"}, * {"value":"Close","onclick":"CloseDoc()"} * ] * }, * "value":"File" * } * * */ /** * String 转换成json */ public void StringToJson(){ jJsonDemo=JSONObject.parseObject(sJsonDemo1); System.out.println("jJsonDemo="+jJsonDemo); } /** * json替换 元素 json转化为字符串 */ public void replaceObject(){ jJsonDemo=JSONObject.parseObject(sJsonDemo2); ((JSONObject) jJsonDemo.get("fruits")).put("red", "watermelon"); System.out.println("jJsonDemo="+jJsonDemo); String sJsonDemo = jJsonDemo.toJSONString(); System.out.println("sJsonDemo="+sJsonDemo); } /** * json 插入map */ @SuppressWarnings("unchecked") public void putMap(){ jJsonDemo=JSONObject.parseObject(sJsonDemo2); HashMap mapCert = new HashMap(); mapCert.put("identType", "Ind01"); //证件种类 mapCert.put("identNo", "130434"); //证件号码 mapCert.put("identOrg", "魏县公安局"); //证件发放机关 mapCert.put("identEffectiveDate", "2019/04/24"); //证件发放日期 mapCert.put("identExpiredDate", "2020/04/24"); //证件到期日期 mapCert.put("openAcctIdent", "1"); jJsonDemo.put("vegetable", JSONObject.toJSON(mapCert)); System.out.println("jJsonDemo="+jJsonDemo); } /** * 判断单条或者多条集合 */ public void singleOrMultiple(){ jJsonDemo=JSONObject.parseObject(sJsonDemo3); Object oFruit = jJsonDemo.get("fruits"); Object oVegetable = jJsonDemo.get("vegetable"); if(oFruit instanceof JSONObject){ System.out.println("水果为单个json对象"); }else if(oFruit instanceof JSONArray){ System.out.println("水果为json集合"); }else{ System.out.println("水果未定义"); } if(oVegetable instanceof JSONObject){ System.out.println("蔬菜为单个json对象"); }else if(oVegetable instanceof JSONArray){ System.out.println("蔬菜为json集合"); }else{ System.out.println("蔬菜未定义"); } } public static void main(String[] args) { JsonDemo1 jsonDemo1 = new JsonDemo1(); jsonDemo1.singleOrMultiple(); } }
诸葛