【问题标题】:java get jsonarray from jsonobject errorjava从jsonobject错误中获取jsonarray
【发布时间】:2014-06-19 07:48:09
【问题描述】:

我正在执行一些递归函数调用,以在 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));

【问题讨论】:

  • @Origineil 我尝试直接放置“update_standard_set”。从 JSONObject 检索时,它给了我同样的错误。所以,为了确保我附加了 JSONArray 对象,我明确定义了“standard_count”并发送了该 JSONArray 的第一个元素。我曾经做过哪些工作,它会抛出错误。有趣的是,它并不总是失败。只偶尔一次:(
  • mgrInfomgrObj 之间是否有任何其他来源正在操纵standard_count 属性,或者mgrObj 与@987654329 所代表的资源不同@。您偶尔会遇到这种失败的事实使我倾向于使用可用上下文中不存在的条件逻辑。

标签: java json


【解决方案1】:

确保没有任何东西在修改您的对象,并且您正确地访问了这些对象。我用来测试的以下代码(在您提供的 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"]

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    相关资源
    最近更新 更多