2014-03-31 73 views
1

我從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列? (等)

將不勝感激:-)

+0

包括導入語句。你使用的是什麼JSON庫? – avgvstvs

+0

看看[this](https://code.google.com/p/json-io/)不確定它會有幫助,但它的值得一讀:)你也可以看看[this]( https://code.google.com/p/google-gson/)祝你好運! – MrHaze

+0

'import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;' –

回答

0

任何幫助,我不認爲你可以這樣直接與正常的JSON庫做。

當您從Excel讀取並構建JSON時,嘗試分別處理「標題」行。你應該嘗試產生一個JSON看起來是這樣的:

{"rows":[ 
    { 
     "TestCase"  : "TC1.01", 
     "SearchString" : "Ferrari", 
     "PageTitle"  : "Ferrari - Google Searching"] 
    }, 
    { 
     "TestCase"  : "TC1.02", 
     "SearchString" : "Toyota", 
     "PageTitle"  : "Toyota - Google Searching"] 
    }, 
    { 
     "TestCase"  : "TC1.03", 
     "SearchString" : "Mazda", 
     "PageTitle"  : "Google"] 
    }, 
    { 
     "TestCase"  : "TC1.04", 
     "SearchString" : "Volvo", 
     "PageTitle"  : "Google"] 
    } 
    ] 
} 

(每個數據行成爲一個地圖,並具有鍵作爲列名)

然後映射這一個POJO應該是微不足道的與任何JSON庫。

0

感謝您的回覆。

我最終做了這樣的工作對我來說。

import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.junit.Before; 
import org.junit.Test; 
import java.io.FileInputStream; 
import java.io.IOException; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class Excel_Example { 

    @Before 
    public void setup(){   
     System.setProperty("webdriver.chrome.driver", "C://temp/chromedriver/chromedriver.exe"); 
    } 

    @Test 
    public void test() throws JSONException { 

     try { 
     // Open the Excel file 
      FileInputStream fis = new FileInputStream("C://temp/testdata.xls"); 
      // Access the required test data sheet 
      HSSFWorkbook wb = new HSSFWorkbook(fis); 
      HSSFSheet sheet = wb.getSheet("testdata"); 
      // Loop through all rows in the sheet 
      // Start at row 1 as row 0 is our header row 
      for(int count = 1;count<=sheet.getLastRowNum();count++){ 
       HSSFRow row = sheet.getRow(count); 
       System.out.println("Running test case " + row.getCell(0).toString()); 

       //Start constructing JSON. 
       JSONObject json = new JSONObject(); 
       json.put("SearchString", row.getCell(1).toString()); 
       json.put("PageTitle", row.getCell(2).toString());     

       // Run the test for the current test data row 
       runTest(json.getString("SearchString").toString(),json.getString("PageTitle").toString()); 

      } 
      fis.close(); 
     } catch (IOException e) { 
      System.out.println("Test data file not found"); 
     }          

}

public static void runTest(String strSearchString, String strPageTitle) { 

     // Start a browser driver and navigate to Google 
     WebDriver driver = new ChromeDriver(); 
     driver.get("http://www.google.com"); 

     // Enter the search string and send it 
     WebElement element = driver.findElement(By.name("q")); 
     element.sendKeys(strSearchString); 
     element.submit(); 

     // Check the title of the page 
     if (driver.getTitle().equals(strPageTitle)) { 
      System.out.println("Page title is " + strPageTitle + ", as expected"); 
     } else { 
      System.out.println("Expected page title was " + strPageTitle + ", but was " + driver.getTitle() + " instead"); 
     } 

     //Close the browser 
     driver.quit(); 
} 
}