Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将json数据读入java对象_Java_Json - Fatal编程技术网

将json数据读入java对象

将json数据读入java对象,java,json,Java,Json,我使用java将Excel中的数据读取到json数组中,如下所示: FileInputStream inp = new FileInputStream("C://temp/testdata.xls"); HSSFWorkbook workbook = new HSSFWorkbook(inp); // Get the first Sheet. Sheet sheet = workbook.getSheetAt(0);

我使用java将Excel中的数据读取到json数组中,如下所示:

 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);
TestCase    SearchString    PageTitle
TC1.01          Ferrari         Ferrari - Google Searching
TC1.02          Toyota          Toyota - Google Searching
TC1.03          Mazda           Google
TC1.04          Volvo           Google
{"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"]
    }
    ]
}
第一行是我的列名

当我打印出我的值时,我得到:

{"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库

在从Excel读取和构造JSON时,请尝试分别处理“header”行。您应该尝试生成如下所示的JSON:

 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);
TestCase    SearchString    PageTitle
TC1.01          Ferrari         Ferrari - Google Searching
TC1.02          Toyota          Toyota - Google Searching
TC1.03          Mazda           Google
TC1.04          Volvo           Google
{"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"]
    }
    ]
}
(每个数据行都成为一个映射,并以键作为列名)


那么,对于任何JSON库来说,将其映射到POJO都应该是微不足道的。

感谢您的回复

我最终做了这样的事情,这对我来说很有用

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");
        }                                     

看一看不确定它是否有用,但值得一读:)你也可以看一看好运!包括导入语句。您使用的JSON库是什么?
import org.JSON.JSONArray;导入org.json.JSONException;导入org.json.JSONObject需要编写代码。大概你想扔掉第一排。然后,您需要决定是要一个包含“cells”的列表,还是要一个由“TC1xx”值键入的映射。确定所需的结构,并在读取数据时创建所需的列表和地图。