2017-03-22 1 views
1
@Path("/getVersion") 
    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    public String getVersion(String getVersionJson) { 
     String version = "", patches = "", connectionStatus = "", output1 = "", output2 = ""; 

     try { 

      JSONObject inputJson = new JSONObject(getVersionJson); 

      String ip = inputJson.getString("ipaddress").trim(); 

      String userName = inputJson.getString("username").trim(); 
      String passWord = inputJson.getString("password").trim(); 
      connectionStatus = getSSHConnection(ip, userName, passWord); 

      if (connectionStatus.equals("Connected")) { 

       //Version Check 
       expect.send("bwshowver" + "\n"); 
       if (expect.expect("$") > -1) { 
        String contt = ""; 
        contt = (expect.before); 
        if (contt != null && contt != "") { 

         contt = contt.replaceAll("\n+", "\n"); 
         contt = contt.replaceAll(" +", " "); 

         String splitter[] = contt.split("\n"); 

         for (int i = 0; i < splitter.length; i++) { 
//       
          if (splitter[i].contains("Patches")) { 
           patches = splitter[i]; 
          } 
          //version 
          if (splitter[i].contains("version")) { 
           version = splitter[i]; 
          } 
//       output1=version.toString(); 
//       output2=patches.toString(); 
//        output3=output1+output2; 
//          
          output1 = contt; 
         } 

        } 

       } else { 
        output1 = "Error in version check"; 
        System.out.println("Error in version check"); 
       } 

      } else { 
       output1 = connectionStatus; 
       System.out.println(connectionStatus); 
      } 
     } catch (Exception e) { 
      output1 = "Error"; 
      //  logger.error("Exception in getVersion Function-ServService Class: " + e.getMessage()); 
     } finally { 
      stopSSH(); 
     } 

     return output3; 
    } 

// getVersion에서 전달되는 문자열은에 액세스하는 방법을 jsonarray elemennts

[{"ipaddress":"10.253.140.116","password":"c0mcast!","username":"bwadmin"},{"ipaddress":"10.253.140.117","password":"bwadmin!","username":"bwadmin"}] 

// 내 요구 사항의 구성은에 여기서 ipaddress 및 암호와 사용자 이름 및 저장 항목의 값에 액세스하는 것입니다 배열을 만들어 ConnectionStatus로 보냅니다.

+0

당신은 이미 필요한 것을 모두 가지고 있습니다. – LppEdd

답변

0
JSONArray jsonArr = new JSONArray(getVersionJson); 

for (int i = 0; i < jsonArr.length(); i++) { 
    JSONObject jsonObj = jsonArr.getJSONObject(i); 
    //Now you can fetch values from each JSON Object 
} 

JSONObject 클래스는 단지 하나의 JSON을 나타냅니다. 이것은 일반적으로 중괄호 {} 안에 있습니다.
예상 입력 getVersionJson이 하나의 JSON 객체 일 경우 코드가 작동합니다.

그러나 입력 문자열 getVersionJson에서 JSON 객체 배열을 얻고 있으므로 JSONArray 클래스를 사용하여 JSON 객체 배열을 처리해야합니다.
[ { }, { }, { }, ... ]

JSONArrayList 연장 일반적이므로 각 JSONObject 값을 읽어 반복 될 수있다.

참조 용 문서는 Interface JsonArray입니다.

+1

이 코드 단편은 질문을 해결할 수 있지만 [설명 포함] (http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)은 게시물의 품질을 향상시키는 데 정말로 도움이됩니다. 앞으로 독자의 질문에 답하고 있으며 코드 제안의 이유를 알지 못할 수도 있습니다. – DimaSan

+0

감사합니다. 나는 설명을 추가했다. – f2001340