【问题标题】:parse json structure from excel sheet into java将excel表中的json结构解析为java
【发布时间】:2014-12-30 12:11:28
【问题描述】:

我有一个需要在 java 中读取的 excel 表中的数据,我能够读取正常内容但无法读取我存储的 json 结构。如何将 json 从 excel 解析为 java?

public class JavaApplication1 {
public static void main(String[] args) {
    try {
      for (int i=0;i<5;i++)
      {
        FileInputStream fileInputStream = new FileInputStream("C://users/user/Desktop/C.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
        HSSFRow row1 = worksheet.getRow(0);

        HSSFCell cellC1 = row1.getCell((short) 2);
        String c1Val = cellC1.getStringCellValue();
        HSSFCell cellD1 = row1.getCell((short) 3);
        double d1Val = cellD1.getNumericCellValue();
        HSSFCell cellE1 = row1.getCell((short) 4);
        String e1Val = cellE1.getStringCellValue();
        System.out.println("C1: " + c1Val);
        System.out.println("D1: " + d1Val);
        System.out.println("E1: " + e1Val);

        JSONObject obj = new JSONObject();

        obj.put("name", new c1Val);

        System.out.print(obj);
                    }
            } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

我的 excel 表包含以下 json:

     A      B                       C                            D        E  
     1      rap   {"type":"int", "minimum":15, "maximum":58}     240     delhi

需要从 c 列读取此数据,我需要从 15 到 58 读取单个变量。我该怎么做?

【问题讨论】:

  • 您的代码有什么问题?
  • @Gagravarr 我无法解析 json 结构,即。 {"type":"int", "minimum:15", "maximum":58} 进入我的程序。我怎样才能做到这一点?通过解析这个结构,我需要在我的程序中读取 15 到 58 之间的值 ..
  • 使用gjson库读取json
  • @swan 我可以执行从以下json结构中读取随机变量的操作吗 {"type":"int", "minimum:15", "maximum":58}
  • 是的,如果您使用 Map get 函数,您可以从该 json 结构中读取任何值

标签: java json excel apache-poi


【解决方案1】:

您可以像这样在一行中将 JSON 解析为 Java 对象,

http://wiki.fasterxml.com/JacksonInFiveMinutes

如果你的 Json String 值在 c1Val 中,那么你的 json 代码将是这样的。

Map<String,Object> c_data = mapper.readValue(c1Val, Map.class);
String minimum = c_data.get("minimum");
String maximum= c_data.get("maximum");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    相关资源
    最近更新 更多