简体   繁体   中英

java get jsonarray from jsonobject error

I'm doing some recursive function call to append data in a JSONObject and count the totals in the end.

My JSON Structure with both JSONObjects and 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

}

I'm having a problem in the final steps of fetching the data from this object and calculating the totals in the below code for " standard_count " statement.

java.lang.Integer cannot be cast to 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();
        }
    }

Code, how I'm populating the above 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));

Make sure that nothing is modifying your object, and that you correctly are accessing the objects. The following code I used to test (on the json object you provided) and it worked correctly:

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());

}

Which gave me the output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM