Java 将json数据读取到excel

Java 将json数据读取到excel,java,json,Java,Json,我有一个JSON文件,格式如下 { "applications": [ { "author": "Appriss, Inc.", "rating": 4.5, "isAvailable": true, "isRecommended": null, "isEndorsed": false, "id": "WfIABNya87qyAWABoDivFQ", "app_name": "MobilePatrol

我有一个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();
}
}
我在行接口的getField和addField方法中遇到错误。首先,这是正确的声明吗?私有静态行=新行;
行来自org.apache.poi.ss.usermodel.Row

您可以使用著名的ApachePOI创建excel文件。它提供了文档链接。你的问题很笼统。当你有一个精确的编程问题时,你应该试着自己去做,然后回到stackoverflow

您的问题可以分为以下三个部分

解析JSON 读取所需的值 在excel中写入数据 对于第一部分,您可以使用各种JSON解析API,还可以参考

对于第二部分,一旦您将数据输入到代码中,您需要能够读取它,为此您需要能够遍历通过使用上述API获得的对象

对于最后一部分,您可以简单地将输出写入CSV格式的文件,然后在Excel中打开它


这个答案可能看起来很模糊,如果您需要澄清,请发表评论

如果您不了解API,请在Java中搜索EXCEL,如果您根本不知道如何使用它,请遵循API通常提供的指南。如果没有,请用问题更新您的问题。就目前而言,询问API或指南是不合适的。请看下面我写的代码。什么代码?JSON还不够,您需要显示您尝试读取、解码和插入值的内容。添加代码writen我添加了编写的代码和我面临的错误。你能帮我吗?