2012-12-18 3 views
3
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { 

    Map countryList = new HashMap(); 

    String str = "http://10.10.10.25/TEPortalIntegration/CustomerPortalAppIntegrationService.svc/PaymentSchedule/PEPL/Unit336"; 

    try { 
     URL url = new URL(str); 

     URLConnection urlc = url.openConnection(); 

     BufferedReader bfr = new BufferedReader(new InputStreamReader(urlc.getInputStream())); 

     String line, title, des; 

     while ((line = bfr.readLine()) != null) { 

      JSONArray jsa = new JSONArray(line); 

      for (int i = 0; i < jsa.length(); i++) { 
       JSONObject jo = (JSONObject) jsa.get(i); 

       title = jo.getString("Amount"); 

       countryList.put(i, title); 
      } 

      renderRequest.setAttribute("out-string", countryList); 

      super.doView(renderRequest, renderResponse); 
     } 
    } catch (Exception e) { 

    } 
} 

liferay 포틀릿 클래스의 json 개체에 액세스하려고하고 있는데 어떤 json 필드의 값 배열을 JSP 페이지에 전달하려고합니다.Java 클라이언트에서 JSON Webservice를 사용하는 방법은 무엇입니까?

+3

JSONArray로 변환하기 전에 전체 응답을 읽어야합니다. 응답의 각 개별 행은 (유효하지 않은) JSON 조각이 될 것이므로 분리하여 구문 분석 할 수 없습니다. – Perception

답변

3

JSON 배열로 변환하기 전에 전체 응답을 읽어야합니다. 이것은 응답의 각 개별 라인이 (부적절한) JSON 프래그먼트가 될 것이기 때문에 격리되어서는 안됩니다. 약간의 수정을하면 코드가 작동하며 아래 내용이 강조 표시됩니다.

// fully read response 
final String line; 
final StringBuilder builder = new StringBuilder(2048); 

while ((line = bfr.readLine()) != null) { 
    builder.append(line); 
} 

// convert response to JSON array 
final JSONArray jsa = new JSONArray(builder.toString()); 

// extract out data of interest 
for (int i = 0; i < jsa.length(); i++) { 
    final JSONObject jo = (JSONObject) jsa.get(i); 
    final String title = jo.getString("Amount"); 

    countryList.put(i, title); 
} 
+0

마지막 줄에 오류가 있습니다. JSONArray jsa = 새 JSONArray (builder.toString()); 지금까지 작동하지 않습니다 – Shibu

+0

가져 오기 org.json.JSONArray를 포함 시키거나 org.json.simple.JSONArray를 가져와야합니까? – Shibu

+0

[org.json.JSONArray] (http://www.json.org/javadoc/org/json/JSONArray.html)입니다. – Perception

관련 문제