JSON格式转换工具

JSON格式转换工具,包括了json正向/逆向的各种转换
bean --> json,json --> bean,string --> json,string --> bean,file -->json, file-->bean


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
/**
 * JSON格式转换工具
 * @author yeno.hhr
 *
 */
public class JsonUtil {
	/**
	 * 以json格式装载文件
	 * @param filePath
	 * @return
	 * @throws FileNotFoundException
	 */
	public static JsonObject loadFile(String filePath) throws FileNotFoundException {
		Reader reader = new FileReader(filePath);
		return JsonParser.parseReader(reader).getAsJsonObject();
	}
	/**
	 * 以指定对象格式装载文件
	 * @param 
	 * @param filePath
	 * @param t
	 * @return
	 * @throws FileNotFoundException
	 */
	public static  T loadFile(String filePath, Type t) throws FileNotFoundException {
		Reader reader = new FileReader(filePath);
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(new JsonReader(reader), t);
	}
	/**
	 * 以指定对象格式装载文件,文件存放程序根目录下
	 * @param 
	 * @param fileName
	 * @param t
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static  T loadClassesFile(String fileName, Type t) throws UnsupportedEncodingException {
		Reader reader = new InputStreamReader(JsonUtil.class.getResourceAsStream(fileName), "utf-8");
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(new JsonReader(reader), t);
	}
	/**
	 * 以指定对象格式装载文件,文件存放在工程根目录下
	 * @param 
	 * @param filePath
	 * @param t
	 * @return
	 * @throws IOException 
	 */
	public static  T loadRootFile(String fileName, Type t) throws IOException {
		Reader reader = new FileReader((new File("./"+fileName)).getCanonicalFile());
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(new JsonReader(reader), t);
	}
	/**
	 * 流文件转bean
	 * @param 
	 * @param reader
	 * @param c
	 * @return
	 */
	public static  T toBean(Reader reader, Class c) {
		Gson gson = new GsonBuilder().create();
		return (T) gson.fromJson(reader, c);
	}
	/**
	 * json格式转bean
	 * @param 
	 * @param json
	 * @param c
	 * @return
	 */
	public static  T toBean(JsonObject json, Class c) {
		Gson gson = new GsonBuilder().create();
		return (T) gson.fromJson(json, c);
	}
	/**
	 * string格式转bean
	 * @param 
	 * @param json
	 * @param c
	 * @return
	 */
	public static  T toBean(String json, Class c) {
		Gson gson = new GsonBuilder().create();
		return  (T) gson.fromJson(json, c);
	}
	/**
	 * json格式转bean
	 * @param 
	 * @param json
	 * @param t
	 * @return
	 */
	public static  T toBean(JsonObject json, Type t) {
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(json, t);
	}
	/**
	 * string格式转bean
	 * @param 
	 * @param json
	 * @param t
	 * @return
	 */
	public static  T toBean(String json, Type t) {
		Gson gson = new GsonBuilder().create();
		return gson.fromJson(json, t);
	}
	/**
	 * 将字符串转换为json对象
	 * @param json
	 * @return
	 */
	public static JsonObject toJson(String json) {
		return JsonParser.parseString(json).getAsJsonObject();
	}
	/**
	 * 将bean转换为json对象
	 * @param bean
	 * @return
	 */
	public static JsonObject toJson(Object bean) {
		String json = (new Gson()).toJson(bean);
		return toJson(json);
	}
	/**
	 * 将bean转换为json对象,按指定对象格式Type
	 * @param bean
	 * @param t
	 * @return
	 */
	public static JsonObject toJson(Object bean, Type t) {
		String json = (new Gson()).toJson(bean, t);
		return toJson(json);
	}
}

 

你可能感兴趣的:(Java,json,gson,bean转json,string转json,map转json)