gpt4 book ai didi

java - 读取json数据到excel

转载 作者:行者123 更新时间:2023-12-02 13:09:59 24 4
gpt4 key购买 nike

我有一个以下格式的 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以键/值对的形式将一些数据(如作者、评级、应用程序名称等)读入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();
}
}

我在 Row 接口(interface)的 getField、addField 方法中遇到错误。首先,它的声明正确吗?私有(private)静态行行=新行();Row来自org.apache.poi.ss.usermodel.Row;

最佳答案

您可以使用著名的 Apache POI 来创建 Excel 文件。可用 here带有文档链接。你的问题很笼统。你应该尝试自己做,并在遇到精确的编程问题时返回 stackoverflow。

关于java - 读取json数据到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com