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