java 使用ObjectMapper实现json文件读取与写入

java 使用ObjectMapper实现json文件读取与写入

需求背景:
跟第三方对接,第三方定时上传json格式的文件至ftp服务器,定时取json文件并解析入库。将数据处理后生成json文件给另外一个系统使用。

  刚开始的思路是,通过IO读取文件成字符串,然后使用ali的fastJSON转换为JSONObject,但是发现报错,原因是我们通过IO获取的字符串格式如下
	{"163":{"MAN":{"vecIn":"99999","vecOut":"88888","baseLineIn":"999998","baseLineOut":"888889","detail":{"电路群名称A":{"vecIn":"10000","vecOut":"10000","baseLineIn":"10000","baseLineOut":"10000"},"电路群名称B":{"vecIn":"20000","vecOut":"20000","baseLineIn":"20000","baseLineOut":"20000"}},"alarm":[{"name":"电路群名称A","type":"电路群流量突降","summary":"测试","starttime":"20210601120000"},{"name":"电路群名称A","type":"电路群中断","summary":"测试","starttime":"20210601120000"},{"name":"电路群名称B","type":"电路群流量突降","summary":"测试","starttime":"20210601120000"}]}}}
	而 FastJson处理可以的字符串格式如下,不想做转义,于是决定使用ObjectMapper实现文件解析
	"{\"163\":{\"MAN\":{\"vecIn\":\"99999\",\"vecOut\":\"88888\",\"baseLineIn\":\"999998\",\"baseLineOut\":\"888889\",\"detail\":{\"电路群名称A\":{\"vecIn\":\"10000\",\"vecOut\":\"10000\",\"baseLineIn\":\"10000\",\"baseLineOut\":\"10000\"},\"电路群名称B\":{\"vecIn\":\"20000\",\"vecOut\":\"20000\",\"baseLineIn\":\"20000\",\"baseLineOut\":\"20000\"}},\"alarm\":[{\"name\":\"电路群名称A\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称A\",\"type\":\"电路群中断\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称B\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"}]}}}"

样例代码
对ObjectMapper使用做简单介绍,其他使用可以参考官方说明
json文件样例:
java 使用ObjectMapper实现json文件读取与写入_第1张图片

生成文件:
java 使用ObjectMapper实现json文件读取与写入_第2张图片
对比会发现,生成的文件没有进行优化展示,暂时没有找到好的方法。以后补充吧。。。

package com.lt.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonParseDemo 
{
	private static String outFile = "C:\\Users\\86136\\Desktop\\writejson.text";
	private static String inFile = "C:\\Users\\86136\\Desktop\\json.text";
	public static void main(String[] args) throws Exception
	{
		//readJsonFile() //读取json文件
		//writeJsonFile();  //生成json文件
		//writeJsonFile2();  //生成json文件其他方式
		testAliJson(); //测试fastjson
	}
	
	private static void writeJsonFile2()  throws Exception
	{
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> obj = getObj();
		JsonNode rootNode = mapper.valueToTree(obj);
		System.out.println(rootNode);
		//写json
		//{"163":{"MAN":{"vecIn":"99999","vecOut":"88888","baseLineIn":"999998","baseLineOut":"888889","detail":{"电路群名称A":{"vecIn":"10000","vecOut":"10000","baseLineIn":"10000","baseLineOut":"10000"},"电路群名称B":{"vecIn":"20000","vecOut":"20000","baseLineIn":"20000","baseLineOut":"20000"}},"alarm":[{"name":"电路群名称A","type":"电路群流量突降","summary":"测试","starttime":"20210601120000"},{"name":"电路群名称A","type":"电路群中断","summary":"测试","starttime":"20210601120000"},{"name":"电路群名称B","type":"电路群流量突降","summary":"测试","starttime":"20210601120000"}]}}}
		mapper.writeTree(mapper.getJsonFactory().createGenerator(new FileOutputStream(outFile)), rootNode);
		
		//效果如下
		//"{\"163\":{\"MAN\":{\"vecIn\":\"99999\",\"vecOut\":\"88888\",\"baseLineIn\":\"999998\",\"baseLineOut\":\"888889\",\"detail\":{\"电路群名称A\":{\"vecIn\":\"10000\",\"vecOut\":\"10000\",\"baseLineIn\":\"10000\",\"baseLineOut\":\"10000\"},\"电路群名称B\":{\"vecIn\":\"20000\",\"vecOut\":\"20000\",\"baseLineIn\":\"20000\",\"baseLineOut\":\"20000\"}},\"alarm\":[{\"name\":\"电路群名称A\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称A\",\"type\":\"电路群中断\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称B\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"}]}}}"
		//mapper.writeValue(Paths.get(outFile).toFile(), rootNode.toString());
		System.out.println("写Json文件执行结束");
	}

	/**
	 * 读取json文件
	 * @throws Exception
	 */
	public static void readJsonFile()  throws Exception
	{
		File file = new File(inFile);
		byte[] b = new byte[1024];
		FileInputStream fis = new FileInputStream(file);
		int size = 0;
		StringBuilder text = new StringBuilder();
		while ((size = fis.read(b, 0, 1024)) > 0)
		{
			text.append(new String(b));
		}
		
		String lst = text.toString().replaceAll("\\s", "").replaceAll("\n", "");
		//初始尝试使用ali的fastjson解析字符串发现报错。
		
		System.out.println(lst);
		//使用这个直接解析json文件
		ObjectMapper mapper = new ObjectMapper();
		JsonNode jsonNode = mapper.readTree(file);
	
		//处理解析的数据
		System.out.println("==================处理解析的数据===================");
		//
		JsonNode $163Node = jsonNode.get("163");
		JsonNode detail = $163Node.get("MAN").get("detail");
		Iterator<JsonNode> it = detail.elements();
		while (it.hasNext())
		{
			Detail detObj = mapper.treeToValue(it.next(), Detail.class);	
			System.out.println(detObj);
		}

	}
	
	/**
	 * 使用mapper写入map.这种写入的数据是紧凑的,不是美化的JSON文件格式
	 * @throws Exception
	 */
	public static void writeJsonFile()  throws Exception
	{
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> obj = getObj();
		
		mapper.writeValue(new FileOutputStream(outFile), obj);
		System.out.println("写Json文件执行结束");
	}
	
	public static Map<String, Object> getObj()
	{
		Map<String, Object> obj = new LinkedHashMap<>();
		Map<String, Object> $163 = new LinkedHashMap<>();
		Map<String, Object> $man = new LinkedHashMap<>();
		$163.put("MAN", $man);
		$man.put("vecIn", "99999");
		$man.put("vecOut", "88888");
		$man.put("baseLineIn", "999998");
		$man.put("baseLineOut", "888889");
		Map<String, Object> $detail = new LinkedHashMap<>();
		Detail deail1 = new Detail("10000", "10000", "10000", "10000");
		Detail deail2 = new Detail("20000", "20000", "20000", "20000");
		$detail.put("电路群名称A", deail1);
		$detail.put("电路群名称B", deail2);
		$man.put("detail", $detail);
		
		List<Alarm> $alarm = new ArrayList<>();
		Alarm a1 = new Alarm("电路群名称A", "电路群流量突降", "测试", "20210601120000");
		Alarm a2 = new Alarm("电路群名称A", "电路群中断", "测试", "20210601120000");
		Alarm a3 = new Alarm("电路群名称B", "电路群流量突降", "测试", "20210601120000");
		$alarm.add(a1);
		$alarm.add(a2);
		$alarm.add(a3);
		$man.put("alarm", $alarm);
		
		obj.put("163", $163);
		return obj;
	}
	
	
	public static void testAliJson()
	{
		String test = "{\"163\":{\"MAN\":{\"vecIn\":\"99999\",\"vecOut\":\"88888\",\"baseLineIn\":\"999998\",\"baseLineOut\":\"888889\",\"detail\":{\"电路群名称A\":{\"vecIn\":\"10000\",\"vecOut\":\"10000\",\"baseLineIn\":\"10000\",\"baseLineOut\":\"10000\"},\"电路群名称B\":{\"vecIn\":\"20000\",\"vecOut\":\"20000\",\"baseLineIn\":\"20000\",\"baseLineOut\":\"20000\"}},\"alarm\":[{\"name\":\"电路群名称A\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称A\",\"type\":\"电路群中断\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"},{\"name\":\"电路群名称B\",\"type\":\"电路群流量突降\",\"summary\":\"测试\",\"starttime\":\"20210601120000\"}]}}}";
		JSONObject json = JSON.parseObject(test);
		System.out.println(json);
	}
}

class Detail
{
	public Detail(String vecIn, String vecOut, String baseLineIn, String baseLineOut) {
		
		this.vecIn = vecIn;
		this.vecOut = vecOut;
		this.baseLineIn = baseLineIn;
		this.baseLineOut = baseLineOut;
	}
	
	
	public Detail() {
		
	}


	private String vecIn;
	private String vecOut;
	private String baseLineIn;
	private String baseLineOut;
	public String getVecIn() {
		return vecIn;
	}
	public void setVecIn(String vecIn) {
		this.vecIn = vecIn;
	}
	public String getVecOut() {
		return vecOut;
	}
	public void setVecOut(String vecOut) {
		this.vecOut = vecOut;
	}
	public String getBaseLineIn() {
		return baseLineIn;
	}
	public void setBaseLineIn(String baseLineIn) {
		this.baseLineIn = baseLineIn;
	}
	public String getBaseLineOut() {
		return baseLineOut;
	}
	public void setBaseLineOut(String baseLineOut) {
		this.baseLineOut = baseLineOut;
	}
	@Override
	public String toString() {
		return "Detail [vecIn=" + vecIn + ", vecOut=" + vecOut + ", baseLineIn=" + baseLineIn + ", baseLineOut="
				+ baseLineOut + "]";
	}
	
}

class Alarm
{
	private String name;
	private String type;
	private String summary;
	private String starttime;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getStarttime() {
		return starttime;
	}
	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
	public Alarm(String name, String type, String summary, String starttime) {
		super();
		this.name = name;
		this.type = type;
		this.summary = summary;
		this.starttime = starttime;
	}
	
	
}

你可能感兴趣的:(工具类,java)