Json字符串转换为java对象的各种实现方法:

http://mengzhengbin520.blog.51cto.com/7590564/1283361

使用json作为配置文件:

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class PatternList {

	private static final String FILE_PATH = "/pattern/";
	private static Gson gson = new Gson();
	private Pattern[] patterns;
	private static Map<String, PatternList> cache = new HashMap<String, PatternList>();

	private PatternList(Pattern[] patterns) {
		this.patterns = patterns;
	}

	public static PatternList load(String fileName) {
		if (cache.containsKey(fileName)) {
			return cache.get(fileName);
		}
		
		InputStreamReader in = new InputStreamReader(PatternList.class.getResourceAsStream(FILE_PATH + fileName));
		JsonReader reader = new JsonReader(in);
		PatternList list = new PatternList((Pattern[]) gson.fromJson(reader, Pattern[].class));
		cache.put(fileName, list);
		try {
			in.close();
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public Pattern[] elements() {
		return patterns;
	}

	public class Pattern {
		private String code;
		private String regex;
		private Param[] params;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getRegex() {
			return regex;
		}

		public void setRegex(String regex) {
			this.regex = regex;
		}

		public Param[] getParams() {
			return params;
		}

		public void setParams(Param[] params) {
			this.params = params;
		}
	}

	public class Param {
		private String id;
		private String expr;

		public String getId() {
			return id;
		}

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


		public String getExpr() {
			return expr;
		}

		public void setExpr(String expr) {
			this.expr = expr;
		}
	}

}
json文件位置:/pxene-telecom/src/main/resources/pattern

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐