public static String read(String txtName, String apiName) {
txtName = "E:/Diff/" + txtName + ".xls";
// 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
File filename = new File(txtName);
if (!filename.exists()) {
return "";
}
String json = "";
// 创建输入流,读取Excel
// InputStream is = new FileInputStream(filename.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = null;
try {
wb = Workbook.getWorkbook(filename);
} catch (BiffException | IOException e) {
e.printStackTrace();
}
// 只有一个sheet,直接处理
// 创建一个Sheet对象
Sheet sheet = wb.getSheet(0);
// 得到所有的行数
int rows = sheet.getRows();
// 越过第一行 它是列名称
for (int j = 1; j < rows; j++) {
// 得到每一行的单元格的数据
Cell[] cells = sheet.getRow(j);
if (cells[2].getContents().equals("") || cells.length < 3) {
continue;
}
if (cells[1].getContents().equals(apiName)) {
json = cells[2].getContents();
break;
}
}
return json;
}
public static void write(String txtName, String apiName, String content) {
WritableWorkbook wwb;
String path = "E:/Diff/";
new File(path).mkdirs();
txtName = path + txtName + ".xls";
File writename = new File(txtName);
// 相对路径,如果没有则要建立一个新的txt文件
try {
boolean flag = writename.createNewFile();
// 创建输入流,读取Excel
InputStream is = new FileInputStream(writename);
// 创建一个Sheet对象
if (flag) {
OutputStream os = new FileOutputStream(writename);
// 创建一个可写的Workbook
wwb = Workbook.createWorkbook(os);
// 创建一个可写的sheet,第一个参数是名字,第二个参数是第几个sheet
WritableSheet sheet = wwb.createSheet(txtName, 0);
sheet.addCell(new Label(0, 0, "Id"));
sheet.addCell(new Label(1, 0, "ApiName"));
sheet.addCell(new Label(2, 0, "Response"));
wwb.write();
wwb.close();
if (os != null) {
os.close();
}
}
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
wwb = Workbook.createWorkbook(writename, wb);
WritableSheet sheet = wwb.getSheet(0);
// 得到所有的行数
int rows = sheet.getRows();
// 越过第一行 它是列名称
for (int j = 0; j < rows; j++) {
// 得到每一行的单元格的数据
Cell[] cells = sheet.getRow(j);
if (cells[2].getContents().equals("") || cells.length < 3) {
continue;
}
if (cells[1].getContents().equals(apiName)) {
sheet.addCell(new Label(0, j, j + ""));
sheet.addCell(new Label(2, j, content));
break;
}
if (j == rows - 1) {
sheet.addCell(new Label(0, rows, (j + 1) + ""));
sheet.addCell(new Label(1, rows, apiName));
sheet.addCell(new Label(2, rows, content));
}
}
wwb.write();
wwb.close();
if (is != null) {
is.close();
}
} catch (WriteException | BiffException | IndexOutOfBoundsException | IOException e) {
e.printStackTrace();
}
}
/*
* public static String getKey(String str, String value) { String key = "";
* try { JSONObject jsonObject = new JSONObject(str); Iterator<?> iterator =
* jsonObject.keys(); while (iterator.hasNext()) { key = (String)
* iterator.next(); if (jsonObject.getString(key).equals(value)) { return
* key; } }
*
* } catch (JSONException e) { e.printStackTrace(); } return key; }
*/
/**
* 通过JSONObject返回value对应的key
* @param jsonObject
* @param value
* @return
*/
public static String getKey(JSONObject jsonObject, String value) {
String keyValue = "";
Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
if (jsonObject.get(key) instanceof String) {
if (((String) jsonObject.get(key)).equals(value)) {
keyValue = key.toString();
break;
}
} else if (jsonObject.get(key) instanceof JSONObject) {
JSONObject innerObject = (JSONObject) jsonObject.get(key);
keyValue = getKey(innerObject, value);
if (!keyValue.equals("")) {
break;
}
} else if (jsonObject.get(key) instanceof JSONArray) {
JSONArray innerObject = (JSONArray) jsonObject.get(key);
keyValue = getKey_(innerObject, key, value);
if (!keyValue.equals("")) {
break;
}
} else if (jsonObject.get(key) instanceof Integer) {
if ((jsonObject.get(key).toString()).equals(value)) {
keyValue = key.toString();
break;
}
}
}
return keyValue;
}
public static String getKey_(JSONArray json1, String key, String value) {
String keyValue = "";
if (json1 != null) {
Iterator<Object> i1 = json1.iterator();
while (i1.hasNext()) {
Object ele = i1.next();
if (ele instanceof JSONObject) {
JSONObject innerObject = (JSONObject) ele;
keyValue = getKey(innerObject, value);
if (!keyValue.equals("")) {
break;
}
} else if (ele instanceof JSONArray) {
JSONArray innerObject = (JSONArray) ele;
keyValue = getKey_(innerObject, key, value);
if (!keyValue.equals("")) {
break;
}
} else if (ele instanceof String) {
String innerObject = (String) ele;
if (innerObject.equals(value)) {
keyValue = key;
break;
}
} else if (ele instanceof Integer) {
Integer innerObject = (Integer) ele;
if (innerObject.toString().equals(value)) {
keyValue = key;
break;
}
}
}
}
return keyValue;
}
/**
* 通过JSONArray获取value对应的key,如果字符串或者整数返回value
* @param json1
* @param value
* @return
*/
public static String getJsonArrayKey(JSONArray json1, String value) {
String keyValue = "";
if (json1 != null) {
Iterator<Object> i1 = json1.iterator();
while (i1.hasNext()) {
Object ele = i1.next();
if (ele instanceof JSONObject) {
JSONObject innerObject = (JSONObject) ele;
keyValue = getKey(innerObject, value);
if (!keyValue.equals("")) {
break;
}
} else if (ele instanceof JSONArray) {
JSONArray innerObject = (JSONArray) ele;
keyValue = getJsonArrayKey(innerObject, value);
if (!keyValue.equals("")) {
break;
}
} else if (ele instanceof String) {
String innerObject = (String) ele;
if (innerObject.equals(value)) {
keyValue = innerObject;
break;
}
} else if (ele instanceof Integer) {
Integer innerObject = (Integer) ele;
if (innerObject.toString().equals(value)) {
keyValue = innerObject.toString();
break;
}
}
}
}
return keyValue;
}
public static void main(String[] args) throws BiffException, IOException {
System.out.println("Diff TEST");
// String str3 = "{\"resultcode\":
// 200,\"beans\":{\"w\":\"hh\"},\"result\":\"ww\"}";
// String str3 =
// "{\"username\":\"tom\",\"age\":18,\"address\":[{\"province\":\"上海市1\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]}";
// String str3 =
// "{\"address\":[{\"province\":\"上海市\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]}";
// System.out.println(getKey(JSONObject.parseObject(str3), "tom"));
String str4 = "[{\"province\":\"上海市1\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]";
String str5 = "[\"province\",\"city\",\"disrtict\"]";
String str6 = "[1,\"city\",\"disrtict\"]";
String jsonStr = "{\"aaa\" : \"111\"}";
Object object = JSON.parse(str6);
if (object instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) object;
System.out.println("JSONObject");
} else if (object instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) object;
System.out.println("JSONArray");
System.out.println(getJsonArrayKey(jsonArray, "1"));
} else {
System.out.println("Neither jsonobject nor jsonarray is jsonStr");
}
}