java - 使用json数据在表单中一起输入多个数据

标签 java json selenium parameterization

[
 {
  "name": "Test",
  "type": "Private",
  "item":[{"itmeNo":"PT-15003C","quantity":"3"},
            {"itmeNo":"PT-15003C","quantity":"3"}],

  "successMsg":"Item(s) added to the job list."
  }  
]

嗨, 我正在使用 json 进行数据参数化。上面是我的 json 数据,我想以一种形式输入此数据,其中我需要一起输入 itemNo 和数量。如何使用 json.data 进行数据参数化来完成此操作? 当存在一个键一值对时,我的代码可以正常工作,但在这种情况下,任何人都可以帮助我获得解决方案吗?我为单个 key 对值编写了以下代码。

 public static Object[][] getData(String path) {
    JSONParser parser = new JSONParser();
    JSONArray jArray = null;
    Object[][] testData = null;
    try {
        jArray = (JSONArray) parser.parse(new FileReader(System.getProperty("user.dir") + path));
        testData = new Object[jArray.size()][1];
        Hashtable<String, String> table = new Hashtable<String, String>();
        int i = 0;
        for (Object obj : jArray) {
            table = new Hashtable<String, String>();
            JSONObject objJson = (JSONObject) obj;
            Set<?> keys = objJson.keySet();
            Iterator a = keys.iterator();
            while (a.hasNext()) {
                String key = (String) a.next();
                String value = (String) objJson.get(key);
                // System.out.print("key : "+key);
                // System.out.println(" value :"+value);
                table.put(key, value);
            }
            testData[i][0] = table;
            i++;
        }
        return testData;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

最佳答案

这很容易。首先创建一个类,让我们说 TestObject.javaItem.java 如下,以适应您的 json 结构。

TestObject.java

import java.util.List;

public class TestObject {
    private String name;
    private String type;
    private List<Item> items;
    private String successMsg;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the type
     */
    public String getType() {
        return type;
    }
    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }
    /**
     * @return the items
     */
    public List<Item> getItems() {
        return items;
    }
    /**
     * @param items the items to set
     */
    public void setItems(List<Item> items) {
        this.items = items;
    }
    /**
     * @return the successMsg
     */
    public String getSuccessMsg() {
        return successMsg;
    }
    /**
     * @param successMsg the successMsg to set
     */
    public void setSuccessMsg(String successMsg) {
        this.successMsg = successMsg;
    }
}

Item.java

public class Item {
    private String itemNo;
    private int qty;

    /**
     * @return the itemNo
     */
    public String getItemNo() {
        return itemNo;
    }
    /**
     * @param itemNo the itemNo to set
     */
    public void setItemNo(String itemNo) {
        this.itemNo = itemNo;
    }
    /**
     * @return the qty
     */
    public int getQty() {
        return qty;
    }
    /**
     * @param qty the qty to set
     */
    public void setQty(int qty) {
        this.qty = qty;
    }

}

现在按如下方式解析您的 json 字符串。下面是示例程序,将帮助您了解基础知识:

测试.java

import java.util.ArrayList;
import java.util.List;

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

public class Test {

    public static void main(String[] args) {
        TestObject testObject = new TestObject();
        try {
            JSONObject jsonObject = new JSONObject("{\"name\": \"Test\","
                                                  + "\"type\": \"Private\","
                                                  + "\"item\":[{\"itmeNo\":\"PT-15003C\",\"quantity\":\"3\"},"
                                                  + "          {\"itmeNo\":\"PT-15003C\",\"quantity\":\"3\"}],"
                                                  + "\"successMsg\":\"Item(s) added to the job list.\"}"); //pass json string

            JSONArray jsonArray = jsonObject.getJSONArray("item"); //get the item jsonarray

            testObject.setName(jsonObject.getString("name"));
            testObject.setType(jsonObject.getString("type"));

            List<Item> items = new ArrayList<>();
            Item item = null;

            //Iterate the item array and add to the itemlist object
            for (int i = 0; i < jsonArray.length() ; i++) { 
                JSONObject jsonItem = jsonArray.getJSONObject(i);
                String itmeNo = jsonItem.getString("itmeNo");
                String quantity = jsonItem.getString("quantity");
                item = new Item();
                item.setItemNo(itmeNo);
                item.setQty(Integer.parseInt(quantity));
                items.add(item);
            } 

            testObject.setItems(items);
            testObject.setSuccessMsg(jsonObject.getString("successMsg")); //Now you will have a testObject which have all values from json.
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}

关于java - 使用json数据在表单中一起输入多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414846/

相关文章:

python - 如何使用 Selenium 将 nba.stats.com 上的页面从 "1"更改为 "All"

java - 另一个 JAR 文件中的抽象模块

java - 解析并更新liferay网页内容

selenium - 如何在webdriver中获取div内的属性值

javascript - 在Javascript中通过id查找对象

javascript - 从另一个 JSON 对象创建 JSON 对象

java - 无法使用 java 从 selenium webdriver 的下拉列表中选择选项

java - 如何在 jshell 中使用/设置截断命令中的选择器?

java - scriptlet 标签 <%= some code %> 和 <# some code %> 之间的确切区别是什么?

javascript - 是否有任何实际理由为 JSON 键使用带引号的字符串?