从Excel表到Java解析JSON结构 [英] parse json structure from excel sheet into java
问题描述
我在excel表数据,我需要在Java中阅读,我能够正常读取内容,但无法读取,我已经存储了JSON结构。如何从Excel中解析JSON到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
如果在 c1Val
您的JSON字符串值,那么你的JSON code还会是这样。
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表到Java解析JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!