Java獲取省市的工具類(讀取json文件)

1.讀取json文件

package com.zycfc.ic.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

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

public class RuleMockUtil {
	private static final Logger log = LoggerFactory.getLogger(RuleMockUtil.class);

	public static String mockSendRule(RuleMockEnum responseType) {
		String retXml = "";
		String fileName = "area/"+responseType.getType() + ".txt";
		PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
		Resource resource = pathResolver.getResource("classpath:" + fileName);
		BufferedReader reader = null;

		try {
			reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));

			for (String e = null; (e = reader.readLine()) != null; retXml = retXml + e) {
				;
			}
		} catch (Exception arg14) {
			log.error("解析Json失敗", arg14);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException arg13) {
					arg13.printStackTrace();
				}
			}

		}

		return retXml;
	}
	public static void main(String[] args) {
		System.out.println(AreaUtils.getProvince("370000"));
		System.out.println(AreaUtils.getCity("370000","370100"));

		
	}
}

2.解析json類

package com.zycfc.ic.util;

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

public class AreaUtils {
	public static String getProvince(String province) {
		if(province==null||"".equals(province)) {
			return "";
		}
		String pJson=RuleMockUtil.mockSendRule(RuleMockEnum.PROVINCE);
		return returnMatch(pJson,province);
	}
	public static String getCity(String province,String city) {
		if(province==null||"".equals(province)) {
			return "";
		}
		if(city==null||"".equals(city)) {
			return "";
		}
		String pJson=RuleMockUtil.mockSendRule(RuleMockEnum.CITY);
		JSONObject parseObject = JSON.parseObject(pJson);
		JSONArray parseArray = JSON.parseArray(parseObject.get(province).toString());
		return returnMatch(parseArray.toJSONString(),city);
	} 
	public static String returnMatch(String pJson,String code) {
		JSONArray parseArray = JSON.parseArray(pJson);

		for(int i = 0; i < parseArray.size(); i++) {
			JSONObject obj = parseArray.getJSONObject(i);			
			if(obj.get("id").equals(code)) {
				Object object = obj.get("name");
				return object.toString();
			}
		}
		return "";
	}
}

3.文件枚舉類

package com.zycfc.ic.util;

public enum RuleMockEnum {
	PROVINCE("province", "省"), CITY("city", "市");

	private String type;
	private String name;

	private RuleMockEnum(String type, String name) {
		this.setType(type);
		this.setName(name);
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章