我有一个以下格式的JSON文件。
{
"applications": [
{
"author": "Appriss, Inc.",
"rating": 4.5,
"isAvailable": true,
"isRecommended": null,
"isEndorsed": false,
"id": "WfIABNya87qyAWABoDivFQ",
"app_name": "MobilePatrol Public Safety App",
"icon_path": "org_5945/android_market_62834/appIcon.png",
"custom_metadata": {
"title": null,
"description": null,
"projects": null,
"category": [
100
],
"user_segment": [
200
],
"aboutApp": null,
"tablet_1_description": null,
"tablet_2_description": null,
"tablet_3_description": null,
"tablet_4_description": null,
"tablet_5_description": null,
"screenshot_1_description": null,
"screenshot_2_description": null,
"screenshot_3_description": null,
"screenshot_4_description": null,
"screenshot_5_description": null,
"endorsement": null,
"developer_description": null,
"developer_website": null
},
"operating_system": "ANDROID",
"app_psk": 62834
},
}
我想使用java以密钥/值对的形式将一些数据(如作者,评级,app_name等)读入excel。写在下面的代码。
public class JsonParseTest {
private static List<String> header = new ArrayList<String>();
private static List<Row> rows = new ArrayList<Row>();
private static Row row ;-- not able to instantiate this
private static int rowsSize;
public static List<String> getHeader() {
return header;
}
public static List<Row> getRows() {
return rows;
}
public static void main(String[] args) throws IOException, ParseException {
try {
// 1.read the json file
JSONObject jsonObject = readJson();
//2.iterate json file
for (Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext(); ) {
String header = (String) iterator.next();
short type = getType(jsonObject, header);
if (type == (short) 2) {
createHeader(header);
addFieldToRow(String.valueOf(jsonObject.get(header)), header);
}
}
createExcelFile();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void iterateJsonObject(JSONObject jsonObject, String header) {
for (Iterator outerIterate = jsonObject.keySet().iterator(); outerIterate.hasNext(); ) {
String key = (String) outerIterate.next();
short type = getType(jsonObject, key);
if (type == (short) 2) {
createHeader(header);
addFieldToRow(String.valueOf(jsonObject.get(key)), key);
}
}
}
public static void iteratorJsonArray(JSONArray jsonArray, String header) {
if (jsonArray != null) {
int index = 0;
for (Iterator iterator = jsonArray.iterator(); iterator.hasNext(); ) {
List<String> beforeItrFields = new ArrayList<String>();
for (String field : ((Object) row).getField()) {
beforeItrFields.add("");
}
if (index == 0) {
rowsSize = getRows().size();
}
JSONObject jsonObject = (JSONObject) iterator.next();
iterateJsonObject(jsonObject, header);
if (!getRows().contains(row)) {
getRows().add(row);
}
reInitializeObj(row);
((Object) row).setField(beforeItrFields);
index++;
} }}
public static void reInitializeObj(Object o) {
if (o instanceof Row) {
row = null;
row = new Row();
}
}
//0:jsonObject,1:jsonArray ,2:key/value
public static Short getType(JSONObject jsonObject, String key) {
if (jsonObject.get(key) instanceof JSONObject)
return (short) 0;
else if (jsonObject.get(key) instanceof JSONArray)
return (short) 1;
else
return (short) 2;
}
public static void createHeader(String key) {
if (!getHeader().contains(key))
getHeader().add(key);
}
public static void addFieldToRow(String value, String key) {
row.addField(value);
}
public static JSONObject readJson() throws IOException, ParseException {
String filePath = "C:\\Users\\skond2\\Desktop\\JSON Files\\PSEID123_APPS.json";
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
return (JSONObject) jsonParser.parse(reader);
}
public static void createExcelFile() throws IOException, IllegalAccessException, InstantiationException {
FileOutputStream fileOut = new FileOutputStream("Apps.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("work log");
HSSFRow row1 = worksheet.createRow((short) 0);
short index = 0;
//create header
for (String header : getHeader()) {
HSSFCell cellA1 = row1.createCell(index);
cellA1.setCellValue(header);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellA1.setCellStyle(cellStyle);
index++;
}
//create rows
index = 1;
for (Row row : getRows()) {
HSSFRow excelRow = worksheet.createRow(index);
short flag = 0;
for (String field : row.getField()) {
HSSFCell cellA1 = excelRow.createCell(flag);
cellA1.setCellValue(field);
flag++;
}
index++;
}
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
}
}
我在getField,Row接口的addField方法中遇到错误。首先,它是正确的声明吗? private static Row row = new Row(); Row来自org.apache.poi.ss.usermodel.Row;
答案 0 :(得分:1)
您可以使用着名的Apache POI创建Excel文件。它有here和文档链接。你的问题非常笼统。您应该尝试自己完成并在有精确的编程问题时返回stackoverflow。
答案 1 :(得分:0)
您的问题可分为以下3个部分
对于第一部分,您可以使用任何广泛的JSON解析API,也可以参考this question
对于第二部分,一旦将数据输入到代码中,您需要能够通读它,为此您需要能够遍历使用上述内容的对象。提到的API。
对于最后一部分,您只需将输出以CSV格式写入文件并在Excel中打开即可。
这个答案可能看似模糊,请在需要澄清时发表评论