1

我是 JSON 新手,并试图实现一些如何解析 json 响应但无法正确处理的示例。

我有响应并使用以下 java 代码:

import org.json.JSONArray;
import org.json.JSONObject;


BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
final JSONObject obj = new JSONObject(br.readLine());

如果我打印JSONObject我得到:

{"xml-fragment":{"workItems":[{"id":{"@id":"251","@version":"1"},"visible":true,"state":"OPENED ","header":{"@priority":"50","flags":{"scheduleStatus":"DURING"},"@startDate":"2013-10-15T12:30:57.493Z","description ":"UserReconcile","@distributionStrategy":"OFFER","name":"UserReconcile","itemContext":{"appName":"NpdaProcess","appInstance":"pvm:0a12iq","activityName": "UserReconcile","appID":" -hYCETV8EeOr4-yR8gXd7g","activityID":"pvm:001im4"}}},{"id":{"@id":"252","@version":"0"},"visible":true,"state":"OFFERED","header":{"@priority":"50","flags":{"scheduleStatus":"DURING "},"@startDate":"2013-10-15T12:31:15.263Z","description":"UserReconcile","@distributionStrategy":"OFFER","name":"UserReconcile","itemContext": {"appName":"NpdaProcess","appInstance":"pvm:0a12ir","activityName":"UserReconcile","appID":"@startDate":"2013-10-15T12:31:15.263Z","description":"UserReconcile","@distributionStrategy":"OFFER","name":"UserReconcile","itemContext":{"appName" :"NpdaProcess","appInstance":"pvm:0a12ir","activityName":"UserReconcile","appID":"@startDate":"2013-10-15T12:31:15.263Z","description":"UserReconcile","@distributionStrategy":"OFFER","name":"UserReconcile","itemContext":{"appName" :"NpdaProcess","appInstance":"pvm:0a12ir","activityName":"UserReconcile","appID":"-hYCETV8EeOr4-yR8gXd7g","activityID":"pvm:001imh"}}}],"totalItems":2,"endPosition":1,"startPosition":0}}

然后我尝试: final JSONArray geodata = obj.getJSONArray("workItems");但它失败了

JSONObject["workItems"] not found.

根据我在 json 文档中的理解,workItems 应该是数组名称还是错误的?

4

1 回答 1

0

您的工作项数组仍在您的 xml 片段中。尝试

final JSONArray geodata = obj.getJSONObject("xml-fragment").getJSONArray("workItems");
于 2013-10-16T09:35:21.937 回答