将excel表中的json结构解析为java [英] parse json structure from excel sheet into java

查看:31
本文介绍了将excel表中的json结构解析为java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 excel 表中有一个数据,我需要用 java 读取,我能够读取正常内容但无法读取我存储的 json 结构.我如何将 json 从 excel 解析为 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. how can i parse json from excel to 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:

my excel sheet contains the following json:

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

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

this data needs to be read, from column c i need to read a single variable from 15 to 58.. how can i do this?

推荐答案

你可以像这样一行一行地将 JSON 解析成 Java 对象,

You can parse JSON into Java object in one line like this,

http://wiki.fasterxml.com/JacksonInFiveMinutes

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

if your Json String value in c1Val then your json code will be like this.

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

这篇关于将excel表中的json结构解析为java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆