1
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, des;
    double title;
    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);
        title = jo.getDouble("NetAmount");

        countryList.put(i, title);
    }
    System.out.println(countryList); /* Giving result if i run in Console*/
  } catch (Exception e) {
            // TODO: handle exception
        }
  renderRequest.setAttribute("out-string", countryList);

上面的代码是从 java 客户端使用 JSON Web 服务。我可以从 Java 控制台应用程序访问它。但是当尝试使用 JSP 或 Liferay 时,它不起作用。在 JSP 中它给出 java.lang.NoClassDefFoundError: org/json/JSONArray。请帮我修复它。我是否需要向库中添加更多 jar 文件以使其在 JSP 中工作?

4

2 回答 2

7

您需要JSONArray按照以下目录结构在 Web 应用程序中添加包含类的 jar 文件:

Tomcat_HOME
  ->
   webapps
     ->
       YourWebAppName
         ->
            WEB-INF
               ->lib
                  ->Here goes your jar file
于 2012-12-19T12:04:52.080 回答
4

json.org.JSONArray您是否考虑过使用 Liferay 的 JSON API而不是使用?

您可以导入:

import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;

他们会做类似的事情:

JSONObject jsonObject = JSONFactoryUtil.createJSONObject(myJSONObjectString);
JSONArray jsonArray = JSONFactoryUtil.createJSONArray(myJSONArrayString);

这样就不需要额外的 JAR 了!

于 2012-12-19T19:10:36.647 回答