繁体   English   中英

使用Java在JSON文件中存在多个数组时,如何从JSON数组文件中输入数据

[英]How to enter data from json array file when multiple array present in json file using java

我正在从json文件读取输入数据。 我的json文件结构是

`{  "testloginpage":[
                    {
                    "username":"hellotest",
                    "password":"password1234",
                    },          
                ],  "emailtest":[
                        {
                        "fromemailaddress":"noreply@test.com",
                        "testemailserver":"111.11.11.1",
                        "testusername":"test",
                        "testpassword":"test",
                        },
                        {
                        "fromemailaddress":"aaaa@test.com",
                        "testemailserver":"22.22.22.2",
                        "testusername":"aaaa",
                        "testpassword":"aaaa",
                        },      ],

}`

我可以通过以下代码循环访问并读取所有数据:

public static void readFromJson() {

        JSONParser parser = new JSONParser();
        try{
            FileReader reader = new FileReader ("<--FilePath--><filenale>.json.txt");
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
            JSONArray login = (JSONArray) jsonObject.get("testloginpage");
            for (int i = 0; i<login.size();i++){
                JSONObject jsonObjectRow = (JSONObject) login.get(i);
                UserName = (String) jsonObjectRow.get("username");
                Password = (String) jsonObjectRow.get("password");
                }
            JSONArray emailtestfields = (JSONArray) jsonObject.get("emailtest");
                    for ( i = 0; i<emailtestfields.size();i++)
                    {
                        System.out.println("The "+i+" elements in email test are" +emailtestfields.get(i));
                    }
                    Iterator i = emailtestfields.iterator();
                    while (i.hasNext()){
                        JSONObject innerObj = (JSONObject) i.next();
                        System.out.println("From email address is "+innerObj.get("fromemailaddress"));
                        fromEmailAddressEmailSettings = (String) innerObj.get("fromemailaddress");
                        System.out.println("test Email Server is  "+innerObj.get("testemailserver"));
                        emailServerEmailSettings = (String) innerObj.get("testemailserver");
                        System.out.println("User name is "+innerObj.get("testusername"));
                        testUserNameEmailSettings = (String) innerObj.get("testusername");
                        System.out.println("test User password is "+innerObj.get("testuserpassword"));
                        testPasswordEmailSettings = (String) innerObj.get("testpassword");
                    }
        }catch (Exception e) {
            System.out.println("Error: "+e);
        }
}

我有一个testng类,在其中创建测试脚本。 当前,脚本仅在表单中输入第二组数据并保存。 我想实现的方案是,我必须从json中读取,因为有多少电子邮件测试记录集可用(例如,其2个),并且比起testng类,我必须首先获取第一组电子邮件测试数据并输入并保存然后选择第二组并输入Web应用程序并保存。 我已经创建了通用功能,可以在文本框中输入有效的数据。 我不知道如何将循环计数带入我的testng测试脚本中,而不是首先输入第一个数据集并保存,然后取第二个数据集,然后输入并保存。

只需将两个JSONObject保存在一个列表中,然后将其传递给您的数据输入方法,例如

List<JSONObject> lst = new ArrayList..
while (i.hasNext()){
JSONObject innerObj = (JSONObject) i.next();
lst.add(innerObj);
}

for (Jsonobject a : lst) {
  putindata(a);
}

public void putindata(JsonObject a) {
   //sendKeys(a.getString("fromemailaddress");
   //...
}

我将json格式更改为{“ appLogin”:{“ userName”:“ test”,“ password”:“ test”,},

        "createNewUser":{
        "UserName":["test1", "test2"],
        "UserPassword":["solution1","solution2"], 
        "UserConfirmPassword":["solution1","solution2"],
        "UserFirstName":["testuser1","testuser2"], 
        "UserLastName":["test1","Hello2"], 
        "UserGroup":"localuser",
        },
}

这是我正在使用的Java代码

static String userName;
static String password;
static String newUserNames[];
static String newUserPasswords[];
static String newUserConfirmPasswords[];
static String newUserFirstNames[];
static String newUserLastNames[];
static String userGroup[];
Public static void readFile() {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("<<Location of ur input json data file>>"));
jsonObject = (JSONObject) obj;
/**
* This section is to read login data from input json file.
*/
JSONObject gveLogin = (JSONObject) jsonObject.get("appLogin");
userName = (String) gveLogin.get("userName");
password = (String) gveLogin.get("password");
/**
* This section is to read create new user from input json file.
*/
JSONObject createNewUser = (JSONObject) jsonObject.get("createNewUser");
JSONArray newUserName = null;
Object newUserNameObj = (createNewUser !=   null &&     
createNewUser.containsKey("UserName")) ? createNewUser.get("UserName") : null;
if (newUserNameObj !=null && newUserNameObj instanceof JSONArray){
newUserName = (JSONArray) newUserNameObj;
}
int newUserNameSize = newUserName.size();
newUserNames = new String[newUserNameSize];
int newUserNameCount;
for (newUserNameCount = 0;newUserNameCount<newUserNameSize; newUserNameCount++){
newUserNames [newUserNameCount] = (String) newUserName.get(newUserNameCount);
System.out.println(newUserNames[newUserNameCount]);
}
.... Same for password, confirm password, first name , last name and for userGroup I have used jsonObject as follows:
newUserGroup = (String) createNewUser.get("userGroup");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM