简体   繁体   English

将Excel工作表中的json结构解析为Java

[英]parse json structure from excel sheet into java

i have a data in excel sheet that i need to read in java, i am able to read normal content but not able to read the json structure that i have stored. 我在Excel工作表中有一个需要在Java中读取的数据,我能够读取常规内容,但无法读取我已存储的json结构。 how can i parse json from excel to java? 如何将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();
    }
  }
}

my excel sheet contains the following json: 我的Excel工作表包含以下json:

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

this data needs to be read, from column ci need to read a single variable from 15 to 58.. how can i do this? 此数据需要读取,从ci列需要读取15到58之间的单个变量。我该怎么做?

You can parse JSON into Java object in one line like this, 您可以像这样将JSON解析为Java对象,

http://wiki.fasterxml.com/JacksonInFiveMinutes http://wiki.fasterxml.com/JacksonInFiveMinutes

if your Json String value in c1Val then your json code will be like this. 如果您在c1Val Json String值,那么您的json代码将像这样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM