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) {

    }
}

jsonLiferayポートレットクラスからオブジェクトにアクセスしようとしていますが、任意のjsonフィールドの値の配列をjspページに渡したいと思います。

4

1 に答える 1

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);
}
于 2012-12-18T07:09:31.810 に答える