Reputation: 535
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);
The above code is to consume JSON web services from java client. I am able to access it from java console application. But when trying with JSP or Liferay its not working. In JSP its giving java.lang.NoClassDefFoundError: org/json/JSONArray. Please help me to fix it. Should i need to add any more jar files to the libraries to make it working in JSP?
Upvotes: 1
Views: 1914
Reputation: 3465
Instead of using json.org.JSONArray
, have you considered using Liferay's JSON API?
You can import:
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
They do something like:
JSONObject jsonObject = JSONFactoryUtil.createJSONObject(myJSONObjectString);
JSONArray jsonArray = JSONFactoryUtil.createJSONArray(myJSONArrayString);
This way there is no additional JAR required!
Upvotes: 4
Reputation: 15664
You need to add the jar file containing JSONArray
class in your web application as per this directory structure:
Tomcat_HOME
->
webapps
->
YourWebAppName
->
WEB-INF
->lib
->Here goes your jar file
Upvotes: 7