How to convert the json of a txt document into a list

One, achieve the effect:

Second, the required jar

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.2</version>
</dependency>

Three, ideas

First read the json of the txt file, and then convert it into a list. During the output process, the output list object is converted to json again, and then converted to an object. The purpose of turning it twice is to not report an error when outputting.

Fourth, the implementation code

public class RegionTest {
	
	private Integer id;
	
	private String province;
	
	private String city;
	
	private String area;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getArea() {
		return area;
	}
 
	public void setArea(String area) {
		this.area = area;
	}
	
	

}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author 
* @createDate 
* @Description:  
*/

public class TxtMain {
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	public static void main(String[] args) {
		//读取json文件
		File file = new File("D:\\test.txt");
		String context = readFile(file);
		System.out.println(context);
		List<RegionTest> list = new ArrayList<RegionTest>();
		List<RegionTest> listTxt = (List<RegionTest>)json2java(context, list.getClass());
		for(int i = 0;i<listTxt.size();i++){
			String jsonObject= java2json(listTxt.get(i));
			RegionTest test = (RegionTest) json2java(jsonObject, RegionTest.class); 
			System.out.println(test.getProvince());
		}
		
	}
	
	
	  public static String readFile(File file) {
			StringBuilder result = new StringBuilder();
			FileReader fileReader = null;
			try {
			    fileReader = new FileReader(file);
			    BufferedReader br = new BufferedReader(fileReader);
			    String s = null;
			    while ((s = br.readLine()) != null) {
				result.append(s);
			    }
			    br.close();
			} catch (Exception e) {
			    e.printStackTrace();
			} finally {
			    try {
				if (fileReader != null) {
				    fileReader.close();
				    fileReader = null;
				}
			    } catch (IOException e) {
				e.printStackTrace();
			    }
			}
			return result.toString();
		    }
	
	
	  public static <T> T json2java(String json, Class<T> zlass) {
			if( json == null ){
				return null;
			}
			try {
				return objectMapper.readValue(json, zlass);
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
	  
	  public static String java2json(Object obj) {
			try {

				return objectMapper.writeValueAsString(obj);
			} catch (JsonProcessingException e) {
				return "";
			}
		}

	
}

 

Guess you like

Origin blog.csdn.net/qq_36138652/article/details/100044108