public class SupplierInterfaceJsonUtil {
/**
* 获取接口的json,转换为字符串
* @param fileName
* @return
*/
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取供应商接口数组
* @return
*/
public static JSONArray getSupplierInterfaces() {
//获取json文件路径,在项目resource目录下
String path = SupplierInterfaceJsonUtil.class.getClassLoader().getResource("supplierInterface.json").getPath();
//将json转换为字符串
String s = readJsonFile(path);
//转换为json数组返回
if(s != null){
JSONObject jsonObject = JSON.parseObject(s);
String outInterface = jsonObject.getString("outInterface");
if(outInterface != null && !outInterface.equals("")){
JSONArray jsonArray = JSON.parseArray(outInterface);
return jsonArray;
}
}
return null;
}
}
xx