2017-09-13 5 views
1

json 파일에서 입력 데이터를 읽습니다. 내 JSON 파일 구조는자바를 사용하여 json 파일에 여러 배열이있는 경우 json 배열 파일에서 데이터를 입력하는 방법

`{ "testloginpage":[ 
        { 
        "username":"hellotest", 
        "password":"password1234", 
        },   
       ], "emailtest":[ 
         { 
         "fromemailaddress":"[email protected]", 
         "testemailserver":"111.11.11.1", 
         "testusername":"test", 
         "testpassword":"test", 
         }, 
         { 
         "fromemailaddress":"[email protected]", 
         "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); 
     } 
} 

는 내가 테스트 스크립트를 생성 M : TestNG의 클래스가 있습니다. 현재 스크립트는 두 번째 데이터 집합 만 양식에 입력하고 저장합니다. 내가 성취하려고하는 시나리오는 emailtest에 대한 레코드 세트가 얼마나 많은지 (예 : 2 번), testng 클래스보다 먼저 emailtest 데이터를 입력하고 입력해야하는 json을 읽어야한다는 것입니다. 저장하고 두 번째 세트를 받아 웹 애플 리케이션에 입력하고 저장하십시오. 나는 잘 작동하는 텍스트 상자에 데이터를 입력하는 제네릭 함수를 만들었습니다. 나는 내 testng 테스트 스크립트에 루프 카운트를 가지고 처음 데이터 셋을 입력하고 저장하고 두 번째 셋팅을 입력하고 저장하는 것보다 어떻게 할 수 있는지 모른다.

답변

0

에 들어가기 되었 나는 { "appLogin"에 JSON 형식을 변경 : { '사용자 이름 ":"테스트 " , "비밀번호": "테스트", },

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

그리고 이것은 내가

0123을 사용하고있는 자바 코드
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"); 
0

그냥 저장 둘 모두 목록에 된 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"); 
    //... 
}