processon脑图转为迅捷脑图导入json

processon脑图转为迅捷脑图导入json

因为自己之前一直在用processon和迅捷画图的在线画图,因为各种原因(穷)就买了一个迅捷的终身会员;
但是前段时间别人给我一些xmind文件,于是我惊喜的发现迅捷的导入不支持xmind文件导入。所以…
我臭不要脸的想把xmind文件直接导入processon,惊喜发现pos和迅捷的json数据神似,但是有些许偏差。所以,自己捣鼓了一下,做了一个pos文件转为迅捷画图(脑图)json的转换代码。
话不多说,直接上代码。

package com.rick;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ProcessonToJson {

	/**
	 * 读取Processon的json数据
	 */
	public static String reader(String filePath) {
		try {
			//读取pos文件
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = bufferedReader.readLine();
				while (lineTxt != null) {
					return lineTxt;
				}
			}
		} catch (UnsupportedEncodingException | FileNotFoundException e) {
			System.out.println("Cannot find the file specified!");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Error reading file content!");
			e.printStackTrace();
		}
		return null;
	}

	public static String proToJson(JSONObject proObject) {
		//去读element数据
		JSONObject elementsObj = proObject.getJSONObject("diagram").getJSONObject("elements");
		//主标题名称
		String fileName = elementsObj.getString("title");
		//子标题
		JSONArray children = elementsObj.getJSONArray("children");
		String id = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 12);
		long created = new Date().getTime();

		JSONObject rootData = new JSONObject();
		rootData.put("id", id);
		rootData.put("created", created);
		rootData.put("text", fileName);

		//组装子标题数据
		JSONArray result = buildData(children);

		JSONObject root = new JSONObject();
		root.put("data", rootData);
		root.put("children", result);

		JSONObject resultJson = new JSONObject();
		resultJson.put("root", root);

		resultJson.put("template", "default");
		resultJson.put("theme", "fresh-blue");
		//version目前没有深究,直接写死。如果发现导入出现问题,建议查看一下目前的版本保持一致即可
		resultJson.put("version", "1.4.43");
		resultJson.put("themeBgColor", null);

		return resultJson.toJSONString();
	}

	//递归组装子标题数据
	public static JSONArray buildData(JSONArray jsonArray) {
		JSONArray result = new JSONArray();
		JSONObject subData;
		JSONObject dataJson;
		for (Object object : jsonArray) {
			// 装配Id和title数据
			JSONObject jsonObject = (JSONObject)object;
			subData = new JSONObject();
			dataJson = new JSONObject();

			subData.put("id", jsonObject.getString("id"));
			subData.put("created", new Date().getTime());
			subData.put("text", jsonObject.getString("title"));
			dataJson.put("data", subData);

			JSONArray childrenArray = jsonObject.getJSONArray("children");
			if (!childrenArray.isEmpty()) {
//				System.out.println("childrenArray = "+childrenArray);
				dataJson.put("children", buildData(childrenArray));
			}
			
			result.add(dataJson);
		}
		return result;
	}

	// 写入文件内容
	public static void write(String filePath, String info) {
		try {
			// 创建字符输出流类对象和已存在的文件相关联
			FileWriter fw = new FileWriter(filePath, false);//这里ture:末尾添加;false:直接覆盖为新数据
			// 为了提高写入的效率,创建字符流的缓冲区
			BufferedWriter bw = new BufferedWriter(fw);
//			 bw.append("在已有的基础上添加字符串");
			bw.write(info + "\r\n");// 写入
			// bw.write("192.168.30.30\r\n");
			bw.close();
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] arges) {
		
		//文件名
		String fileName = "Dubbo课题";

		//读取文件路径
		String readPath = "C:\\Users\\LiRui\\Desktop\\JsonFile\\"+fileName+".pos";
		//读取pos文件
		String proString = reader(readPath);
//		System.out.println(proString);

		JSONObject proObject = JSONObject.parseObject(proString);

		//生成转换文件内容
		String jsonStr = proToJson(proObject);

//		System.out.println(jsonStr);
		//写入文件路径
		String writePath = "C:\\Users\\rick1024\\Desktop\\JsonFile\\"+fileName+".json";
		//写出json文件
		write(writePath, jsonStr);
	}
}
  • 直接服用,亲测有效
发布了17 篇原创文章 · 获赞 6 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/keenstyle/article/details/104259027