0

I am using import net.sf.json.JSONObject

My object is like this:

[{
    "courseId": "AUTO_CAD",
    "strDate": "02-04-2012"
 }, {
    "courseId": "CNC_PROG",
    "strDate": "03-04-2012"
}]

Please help me how to access this in java service

//The code that i have tried.

List<JSONObject> jsonList = new ArrayList<JSONObject>(); 
jsonList = (List<JSONObject>) context.get("courseData"); // courseDate is my json object
JSONObject obj = jsonList.get(0); 
Debug.log("courseStartDate ="+ obj.getJSONObject("courseId")); 

//Using this gives me the following error. org.ofbiz.webapp.event.EventHandlerException: Service invocation error (java.lang.String cannot be cast to net.sf.json.JSONObject)

Vicky
  • 657
  • 1
  • 8
  • 16
  • what do u mean by java service.. webservice ? – Digital Alchemist Mar 21 '14 at 08:08
  • What have you tried so far, can you show your code and tell us why it's not working? – CodingIntrigue Mar 21 '14 at 08:08
  • possible duplicate of [JavaScript type arrays in JAVA](http://stackoverflow.com/questions/22497518/javascript-type-arrays-in-java) – aravind Mar 21 '14 at 08:13
  • Yeah, Its a web service. – Vicky Mar 21 '14 at 08:25
  • based on your error below, it seems like context.get("courseData") returns a list of string, not jsonObject. You can convert these strings via JSONObject.fromObject(list.get(i)) – Robbie Mar 21 '14 at 09:02
  • @Robbie I think you are right. printing context.get("courseData") give me {object Object],[object Object}. printing (context.get("courseData")).get(0) give me object Object],[object Object. Now printing ** JSONObject.fromObject(courseList.get(0))** gives me following error: **(A JSONObject text must begin with '{' at character 1 of object Object],[object Object) ** – Vicky Mar 21 '14 at 10:07

3 Answers3

0

You can do this:

JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);
System.out.println(pilot.getString("aKey"));
Nando
  • 813
  • 2
  • 8
  • 18
0

Here you have an ArrayList yourObject

for (int i = 0; i < yourObject.size(); i++) { 
 System.out.println(yourObject.get(i).getCourseId());
 System.out.println(yourObject.get(i).getStrDate());
}
Franck
  • 1,354
  • 12
  • 17
  • It is not really as simple as you might think :P In Java you have to parse each piece of data individually using a JSON library. – sybear Mar 21 '14 at 08:41
  • @Jari I have imported net.sf.json.JSONObject. How can I use this to parse each piece of data. Any example that you can give. would be great. – Vicky Mar 21 '14 at 08:45
  • I thought he is able to let the code do the marshalling of the object with something like public class YourObjectList extends ArrayList { } And directly get the YourObjectList from the requestbody instead of getting the jsonobject – Franck Mar 21 '14 at 08:47
0

Have not tested yet the code, but worth a try:

JSONArray courses = new JSONArray(json);

JSONObject first = courses.getJSONObject(0); //Or access in a loop using courses.length()
Debug.log("CourseID: "+ first.getString("courseId"));
sybear
  • 7,837
  • 1
  • 22
  • 38
  • Using this gives me the following error: javolution.util.FastList cannot be cast to net.sf.json.JSONArray. – Vicky Mar 21 '14 at 10:02
  • printing context.get("courseData") give me {object Object],[object Object}. printing (context.get("courseData")).get(0) give me object Object],[object Object. Now printing ** JSONObject.fromObject(courseList.get(0))** gives me following error: **(A JSONObject text must begin with '{' at character 1 of object Object],[object Object). – Vicky Mar 21 '14 at 10:23