我从Excel中使用的Java成JSON数组像这样读取数据:阅读JSON数据到Java对象
FileInputStream inp = new FileInputStream("C://temp/testdata.xls");
HSSFWorkbook workbook = new HSSFWorkbook(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);
myvalue = json.toString();
System.out.println(myvalue);
我的excel文件看起来是这样的:
TestCase SearchString PageTitle
TC1.01 Ferrari Ferrari - Google Searching
TC1.02 Toyota Toyota - Google Searching
TC1.03 Mazda Google
TC1.04 Volvo Google
第一行是我的专栏名。
当我打印我的价值观了,我得到这样的:
{"rows":[{"cell":["TestCase","SearchString","PageTitle"]},{"cell":["TC1.01","Ferrari","Ferrari - Google Searching"]},{"cell":["TC1.02","Toyota","Toyota - Google Searching"]},{"cell":["TC1.03","Mazda","Google"]},{"cell":["TC1.04","Volvo","Google"]}]}
如何映射在Java中的数据列的名字呢?例如:我如何映射与Ferarri的SearchString列? (等)
将不胜感激:-)
包括导入语句。你使用的是什么JSON库? – avgvstvs
看看[this](https://code.google.com/p/json-io/)不确定它会有帮助,但它的值得一读:)你也可以看看[this]( https://code.google.com/p/google-gson/)祝你好运! – MrHaze
'import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;' –