0

我有以下 json jsonobject。我可以阅读一些但不是所有的属性。如果我打印 JSONObj 我得到: JSONObj :

{
   "@priority":"50",
   "flags":{
      "scheduleStatus":"DURING"
   },
   "@startDate":"2013-10-15T12:30:57.493Z",
   "description":"UserReconcile",
   "@distributionStrategy":"OFFER",
   "name":"UserReconcile",
   "itemContext":{
      "appName":"MyProcess",
      "appInstance":"pvm:0a12iq",
      "activityName":"UserReconcile",
      "appID":"_-hYCETV8EeOr4-yR8gXd7g",
      "activityID":"pvm:001im4"
   }
}

我能够获得@priority、description 和其他一些的值。我无法获得最后一个键“activityID”。

我的值正常的代码是:

priority=jsonObj.getString("@priority");
startDate =jsonObj.getString("@startDate");
workDesc=jsonObj.getString("description");

这失败了:

amxref = jsonObj.getString("activityID");
4

2 回答 2

2

这是您的 JSON 的扩展视图:

{
    "@priority": "50",
    "flags": {
        "scheduleStatus": "DURING"
    },
    "@startDate": "2013-10-15T12:30:57.493Z",
    "description": "UserReconcile",
    "@distributionStrategy": "OFFER",
    "name": "UserReconcile",
    "itemContext": {
        "appName": "MyProcess",
        "appInstance": "pvm:0a12iq",
        "activityName": "UserReconcile",
        "appID": "_-hYCETV8EeOr4-yR8gXd7g",
        "activityID": "pvm:001im4"
    }
}

你能发现问题吗?

activityID不是你的jsonObj对象的关键。

你应该做:

JSONObject itemContext = jsonObj.getJSONObject("itemContext");
String activityID = itemContext.getString("activityID");
于 2013-10-17T13:11:19.823 回答
0
{
   "@priority":"50",
   "flags":{
      "scheduleStatus":"DURING"
   },
   "@startDate":"2013-10-15T12:30:57.493Z",
   "description":"UserReconcile",
   "@distributionStrategy":"OFFER",
   "name":"UserReconcile",
   "itemContext":{
      "appName":"MyProcess",
      "appInstance":"pvm:0a12iq",
      "activityName":"UserReconcile",
      "appID":"_-hYCETV8EeOr4-yR8gXd7g",
      "activityID":"pvm:001im4"
   }
}

这是您格式化的 JSON 响应,您正在获取的值

priority=jsonObj.getString("@priority");
startDate =jsonObj.getString("@startDate");
workDesc=jsonObj.getString("description");

因为它们在 java 对象中直接可用

amxref = jsonObj.getString("activityID");

失败,因为这是在名为的对象内:itemContext

现在请将其转换为 JSONObject 然后对其进行迭代以获得所需的元素

于 2013-10-17T13:13:18.477 回答