java从jsonobject错误获取jsonarray

时间:2014-06-18 22:17:35

标签: java json

我正在进行一些递归函数调用,以便在JSONObject中追加数据并最终计算总数。

我的JSON结构包含JSONObjects和JSONArray

{
"total": 247,
"passed": 247,
"failed": 0,
"standard_count": [
    "LINK_SHUT_NOSHUT",
    "LC_RELOAD",
    "VDC_RELOAD",
    "LINK_FLAP",
    "SWOVER"
],
"submissionFlag": 2,
"headCount": 6

}

{
"total": 0,
"passed": 0,
"failed": 0,
"standard_count": [],
"submissionFlag": 0,
"headCount": 4

}

我在从此对象获取数据的最后步骤中遇到问题,并在下面的代码中计算“ standard_count ”语句中的总计。

  

java.lang.Integer无法强制转换为org.json.JSONArray

while(managerKeys.hasNext()){
        String manager = managerKeys.next();
        try {
            JSONObject tempObj = (JSONObject) (trendsSet.get(manager));
            JSONObject mgrObj = (JSONObject) (tempObj.get(manager));
            JSONObject newMgrObj = new JSONObject();
            double total = mgrObj.getDouble("total");
            double passed = mgrObj.getDouble("passed");
            double failed = mgrObj.getDouble("failed");
            double headCount = mgrObj.getDouble("headCount");
            double subCount = mgrObj.getDouble("submissionFlag");
            //org.json.JSONArray standard_count = mgrObj.getJSONArray("standard_count");
            org.json.JSONArray standard_count = (org.json.JSONArray) mgrObj.get("standard_count");
      .....
      //doing all the calculations
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

代码,我如何填充上面的JSONObject:

org.json.JSONArray standard_count = new org.json.JSONArray();
org.json.JSONArray sub_mgr_count = subMgrInfo.getJSONArray("standard_count");
org.json.JSONArray mgr_count = mgrInfo.getJSONArray("standard_count");
Set<String> update_standard_set = new HashSet<String>();

for (int i = 0; i < mgr_count.length(); i++) {
    update_standard_set.add((String) mgr_count.get(i));
}
for (int i = 0; i < sub_mgr_count.length(); i++) {
    update_standard_set.add((String) sub_mgr_count.get(i));
}
standard_count.put(update_standard_set);

mgrInfo.put("total", mgrInfo.getInt("total") + total);
mgrInfo.put("passed", mgrInfo.getInt("passed") + passed);
mgrInfo.put("failed", mgrInfo.getInt("failed") + failed);
mgrInfo.put("headCount", mgrInfo.getInt("headCount") + headCount + 1);
mgrInfo.put("submissionFlag", mgrInfo.getInt("submissionFlag") + submissionFlag);
mgrInfo.remove("standard_count");
mgrInfo.put("standard_count", standard_count.get(0));

1 个答案:

答案 0 :(得分:0)

确保没有任何内容正在修改您的对象,并确保您正在访问这些对象。我用以下代码测试(在你提供的json对象上)并且它正常工作:

public static void main (String args[]) throws JSONException{

    String jsonString = "{ \"total\": 247, \"passed\": 247, \"failed\": 0, \"standard_count\": [ \"LINK_SHUT_NOSHUT\", \"LC_RELOAD\", \"VDC_RELOAD\", \"LINK_FLAP\", \"SWOVER\" ], \"submissionFlag\": 2, \"headCount\": 6 }";

    JSONObject newMgrObj = new JSONObject(jsonString);
    JSONArray standard_count = newMgrObj.getJSONArray("standard_count");

    System.out.println(standard_count.toString());

}

这给了我输出:

["LINK_SHUT_NOSHUT","LC_RELOAD","VDC_RELOAD","LINK_FLAP","SWOVER"]