【问题标题】:Converting Excel to JSON [closed]将 Excel 转换为 JSON [关闭]
【发布时间】:2012-07-05 12:02:12
【问题描述】:

我已经阅读了一个使用 apache-poi 的 excel 文档。 具有以下记录的excel文档:

A1 A2 A3 A4

A1 A2 B3 B4

我想将它们转换成 JSON 数组,例如

{ A1 : {A2 : {A3 : {A4 : some_value } } , {B3 : {B4 : some_value } } } }

实际上它很容易转换为 XML。 请告诉我如何解决这个问题。 只有提示就足够了。

【问题讨论】:

标签: java json arrays


【解决方案1】:

可以参考以下代码:

FileInputStream inp = new FileInputStream( file );
Workbook workbook = WorkbookFactory.create( inp );

// Get the first Sheet.
Sheet sheet = workbook.getSheetAt( 0 );

    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for ( Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext(); )
    {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for ( Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext(); )
        {
            Cell cell = cellsIT.next();
            cells.put( cell.getStringCellValue() );
        }
        jRow.put( "cell", cells );
        rows.put( jRow );
    }

    // Create the JSON.
    json.put( "rows", rows );

// Get the JSON text.
return json.toString();

【讨论】:

  • 非常感谢UVM :) 我也设法解决了这个问题
【解决方案2】:

在获取 Java 对象中的数据后,您可以使用这个简单的 JSON 库创建 JSON

http://code.google.com/p/google-gson/

但是你必须在 java 对象中创建那种结构。

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 2012-03-22
    • 2012-12-25
    • 2011-05-07
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多