alibaba Fastjson的使用

Fastjson 源码地址:https://github.com/alibaba/fastjson

Fastjson 中文 Wiki:https://github.com/alibaba/fastjson/wiki/Quick-Start-CN

Fastjson API文档:https://www.w3cschool.cn/fastjson/fastjson-beantoarray.html

 

   alibaba Fastjson的使用_第1张图片

一、引入依赖

1)FastJson 是用于java后台处理json格式数据的一个工具包

2)FastJson 对于json格式字符串的解析主要用到了一下三个类:

    (1)JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
    (2)JSONObject:fastJson提供的json对象。
    (3)JSONArray:fastJson提供json数组对象。

3)maven 引入依赖

  		
  
    com.alibaba  
    fastjson  
    1.2.47  

二、测试demo

   注意:JSON字符串是有格式要求的,必须为键值对形式,不是任意的字符串。

public class User implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private int id;
	private String username;
	private String pazzword;
	private String sex;
	private Date birthday;
	private Integer status; //0:未激活, 1:激活
	private Long ordinal;   //序号

    //get / set
}

1、javaBean,list,array 转 json string

      String jsonString = JSON.toJSONString(object);

/**
	 *  javaBean,list,array convert to json string
	 * 
	 * @param obj
	 * @return
	 */
	public static String obj2json(Object obj) {
		return JSON.toJSONString(obj);
	}

	public static void main(String[] args) {
		User user = new User();
		user.setId(100);
		user.setUsername("李四");
		user.setPazzword("lisi123");
		user.setBirthday(new Date());
		user.setSex("男");
		user.setStatus(1);
		user.setOrdinal(10L);
		
		User user2 = new User();
		user2.setId(101);
		user2.setUsername("王五");
		user2.setPazzword("wangwu123");
		user2.setBirthday(new Date());
		user2.setSex("女");
		user2.setStatus(1);
		user2.setOrdinal(11L);
		
		System.out.println("=javaBean=\n" + obj2json(user));
		
		ArrayList list = new ArrayList<>();
		list.add(user);
		list.add(user2);
		System.out.println("=list=\n" + obj2json(list));
		
		Object[] array = new Object[2];
		array[0] = user;
		array[1] = user2;
		
		System.out.println("=array=\n" + obj2json(array));		
	}

 

2、json string 转 javaBean对象

    T t  =  JSON.parseObject(jsonString, T.class);

	public static void main(String[] args) {		
		String jsonStr = "{\"birthday\":1554885019427,\"id\":100,\"ordinal\":10,\"pazzword\":\"lisi123\",\"sex\":\"男\",\"status\":1,\"username\":\"李四\"}";
		String jsonStr2 = "{\"birthday\":1554885019427,\"id\":100,\"ordinal\":10,\"pazzword\":\"lisi123\",\"sex\":\"男\",\"status11\":1}";
		User user3 = JSON.parseObject(jsonStr, User.class);
		
		System.out.println("=user=\n" + user3);
		System.out.println("=user=\n" + JSON.parseObject(jsonStr2, User.class));
	}

 

  注意:javaBean对象 在 json string 中无对应字段时,值为 null

 

3、json string 转 JSONObject

     JSONObject jsonObject  = JSON.parseObject(jsonString);

    String jsonStr = "{\"birthday\":1554885019427,\"id\":100,\"ordinal\":10,\"pazzword\":\"lisi123\",\"sex\":\"男\",\"status\":1,\"username\":\"李四\"}";
    JSONObject jsonObject  = JSON.parseObject(jsonStr);
    System.out.println(jsonObject);
    System.out.println(jsonObject.get("sex"));

 

 注意:JSONObject 可以通过键值对来获取值

 

三、复杂 jsonStr 的处理

之前有个业务小功能使用到了

	public static void main(String[] args) {
		List> nodesList  = new ArrayList<>();
		
		String jsonStr = "{\r\n" + 
				"    \"title\":\"事项流程图\",\r\n" + 
				"    \"nodes\":{\r\n" + 
				"	\"1553438226938\":{\"name\":\"开始\",\"left\":267,\"top\":88,\"type\":\"start round mix\",\"width\":28,\"height\":28,\"alt\":true},\r\n" + 
				"	\"1553438229007\":{\"name\":\"结束\",\"left\":277,\"top\":412,\"type\":\"end round mix\",\"width\":28,\"height\":28,\"alt\":true},\r\n" + 
				"	\"1553438231623\":{\"name\":\"节点1\",\"left\":\"237\",\"top\":\"169\",\"type\":\"task\",\"width\":\"104\",\"height\":\"28\",\"alt\":true,\"model\":\"node\",\"xm\":\"张三\",\"xb\":\"男\"},\r\n" + 
				"	\"1553438232544\":{\"name\":\"节点2左\",\"left\":\"107\",\"top\":\"251\",\"type\":\"task\",\"width\":\"104\",\"height\":\"28\",\"alt\":true,\"model\":\"node\",\"xm\":\"李四\",\"xb\":\"女\"},\r\n" + 
				"	\"1553438233100\":{\"name\":\"节点2右\",\"left\":\"382\",\"top\":\"251\",\"type\":\"task\",\"width\":\"104\",\"height\":\"28\",\"alt\":true,\"model\":\"node\",\"xm\":\"王五\",\"xb\":\"男\"},\r\n" + 
				"	\"1553438235571\":{\"name\":\"节点3\",\"left\":\"242\",\"top\":\"333\",\"type\":\"task\",\"width\":\"104\",\"height\":\"28\",\"alt\":true,\"model\":\"node\",\"xm\":\"赵柳\",\"xb\":\"不详\"}\r\n" + 
				"    },\r\n" + 
				"    \"lines\":{\r\n" + 
				"	\"1553438237966\":{\"type\":\"sl\",\"from\":\"1553438226938\",\"to\":\"1553438231623\",\"name\":\"\",\"dash\":false},\r\n" + 
				"	\"1553438238928\":{\"type\":\"sl\",\"from\":\"1553438231623\",\"to\":\"1553438233100\",\"name\":\"\",\"dash\":false},\r\n" + 
				"	\"1553438240303\":{\"type\":\"sl\",\"from\":\"1553438231623\",\"to\":\"1553438232544\",\"name\":\"\",\"dash\":false},\r\n" + 
				"	\"1553438241365\":{\"type\":\"sl\",\"from\":\"1553438232544\",\"to\":\"1553438235571\",\"name\":\"\",\"dash\":false},\r\n" + 
				"	\"1553438243677\":{\"type\":\"sl\",\"from\":\"1553438233100\",\"to\":\"1553438235571\",\"name\":\"\",\"dash\":false},\r\n" + 
				"	\"1553438245066\":{\"type\":\"sl\",\"from\":\"1553438235571\",\"to\":\"1553438229007\",\"name\":\"\",\"dash\":false}\r\n" + 
				"    },\r\n" + 
				"    \"areas\":{},\r\n" + 
				"    \"initNum\":13\r\n" + 
				"}";
		
		JSONObject jsonObject  = JSON.parseObject(jsonStr);
		String jsonNodesStr = jsonObject.getString("nodes");
		String jsonLinesStr = jsonObject.getString("lines");
		//jsonStr -- Object -- Map
		Map nodesMap = (Map) JSON.parse(jsonNodesStr); 
		Map linesMap = (Map) JSON.parse(jsonLinesStr);
		
		for (Entry entry : linesMap.entrySet()) {  
			// 根据 lines 的 lineId 关系获取单个节点的有用信息添加到 auditList
			acquireNodeByLineId(nodesList,nodesMap,linesMap,entry.getKey());
		}  
		
		// 将 auditList 按节点大小顺序重新排序
		Collections.sort(nodesList, new Comparator>() {
			@Override
			public int compare(Map o1, Map o2) {
				String lineToId = String.valueOf(o1.get("lineToId"));
				String lineToId2 = String.valueOf(o2.get("lineToId"));
				return lineToId.compareTo(lineToId2);
			}
		});
		
		System.out.println(jsonObject.get("title"));
		for (Map map : nodesList) {
			System.out.println("nodesList == "+ map.toString());
		}
	}
	
	/**
	 * 根据 lines 的 lineId 关系获取单个节点的有用信息添加到 list 对象中
	 * @param auditorList 
	 * @param nodesMap
	 * @param linesMap
	 * @param lineId
	 * @return
	 */
	private static void acquireNodeByLineId(List> nodesList, Map nodesMap, Map linesMap, String lineId) {
		String jsonLineStr = String.valueOf(linesMap.get(lineId)); 
		String lineFromId = JSON.parseObject(jsonLineStr).getString("from");
		String lineToId = JSON.parseObject(jsonLineStr).getString("to");
		//获取单个节点的信息
		String jsonNodeStr = String.valueOf(nodesMap.get(lineToId)); 
		String nodeName = JSON.parseObject(jsonNodeStr).getString("name");
		String nodeXm = JSON.parseObject(jsonNodeStr).getString("xm");
		String nodeXb = JSON.parseObject(jsonNodeStr).getString("xb");
		Map map = new HashMap<>();
		if(!nodeName.equals("结束")) {
			map.put("nodename", nodeName);
			map.put("nodexm", nodeXm);
			map.put("nodexb", nodeXb);
			map.put("lineFromId", lineFromId);
			map.put("lineToId", lineToId);
			nodesList.add(map);
		}
	}

  alibaba Fastjson的使用_第2张图片

     alibaba Fastjson的使用_第3张图片

单流程线不会出现着各种情况,由于是 多流程线,会多出现一个 节点3, 后面可根据需求进一步处理 nodesList 数据

 

参考文章:全解析史上最快JSON解析库

ends

你可能感兴趣的:(Common,alibaba,Fastjson的使用)