无法读取所有JSON属性

时间:2013-10-17 13:03:52

标签: java json

我有以下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");

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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");

失败,因为它位于Object内: itemContext

现在请将此转换为JSONObject,然后迭代它以获得所需的元素