我從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;' –