简体   繁体   English

我有一个JSON对象。 我想在Java服务中访问。 我正在尝试解析

[英]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.

相关问题 我正在从Web服务接收pdf格式的字符串。 我想在单击按钮时为其生成pdf - I am receiving a pdf format string from a web service. I want to generate a pdf for it on button click 我在 Object 中得到 json 响应,但我想在 Json 数组中 - I am gettion json response in Object but i want to be in Json Array 试图将我的 Json String 数据映射到一个对象。 我正在使用 Gson 来解析数据。 数据被解析但是当我给空间值时它给了我异常 - trying to Map my Json String data to an object. I'm using Gson to parse the data. Data get parsed but when i give Space in Value it gives me Exception 我想从URL解析JSON对象(不使用JSON数组) - I want to parse JSON Object from URL ( not using JSON Array) 我有一个带有 1 个 setter 方法的抽象类 - 参数是一个对象。 我想要一个通用的<T>采用扩展它的类的类型的参数 - I have an abstract class w/ 1 setter method - parameter is an object. I want a generic <T> parameter that takes on the type of the class extending it 我有包含其他对象列表的json对象,并想在Java的HashMap中将其转换 - I have json object with contain a list of other object and want to convert it in HashMap in java 然后,我尝试使用Jackson API两次解析json输入流,并得到java.io.EOFException - I am then trying to use the Jackson API to parse the json input stream twice and I get an java.io.EOFException 我正在尝试从位于数组中的特定 JSON 对象中检索数据,但我无法访问“值”对象中的各个项目 - I am trying to retrieve data from a particular JSON object situated in an array, but i cannot access the individual items within the "value" object 我正在尝试访问相机 - I am trying to access the camera 如何使用mongoDB查询访问json文档的成员值? 我正在使用Java,但有一个空指针异常 - How to access to members value of a json document with mongoDB query? I am working with Java, I have a null pointer exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM