[英]I have a json object. I want to access in java service. I am trying to parse it
I am using import net.sf.json.JSONObject 我正在使用导入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 请帮助我如何在Java服务中访问它
//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)
org.ofbiz.webapp.event.EventHandlerException:服务调用错误(java.lang.String无法转换为net.sf.json.JSONObject)
You can do this: 你可以这样做:
JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);
System.out.println(pilot.getString("aKey"));
Here you have an ArrayList yourObject 在这里,您有一个ArrayList yourObject
for (int i = 0; i < yourObject.size(); i++) {
System.out.println(yourObject.get(i).getCourseId());
System.out.println(yourObject.get(i).getStrDate());
}
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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.