0

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));
2
  • @Origineil I tried putting "update_standard_set" directly. It gave me the same error while retrieving from JSONObject. So, just to make sure I'm appending JSONArray object I'm explicitly defining "standard_count" and sending the first element of that JSONArray. Which ever work around I do, its throwing the error. Interestingly its not failing always. Only once in a while :( Commented Jun 18, 2014 at 22:32
  • Between the point of mgrInfo and mgrObj is there a possibility that any other source is manipulating the standard_count property or that mgrObj wouldn't be the same resource represented by mgrInfo. The fact that you only get this failure on occasion leans me toward so conditional logic that is absent from the available context. Commented Jun 18, 2014 at 23:11

1 Answer 1

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.