androidjsonjsonexception

Getting JSONException?


I was trying to parse the below json and caught JSONException at line 150(marked in code) and I am unable to figure out why it happened. Objective is to find list of tests of a particular package and store it in an arraylist.

When I passed screen_package as second arguement then log message was : Value screen_package of type java.lang.String cannot be converted to JSONObject. As per my understanding, we can pass String object in the constructor of JSONObject. Please correct me if I am wrong. Have a look

{
  "package_name": {
    "camera_package": {
      "RearCamera": {
        "test_name": "RearCamera",
        "test_type": "Mandatory",
        "visibility": "true",
        "report_name": "RearCamera",
        "test_category": "manual",
        "order": "42"
      },
      "FrontCamera": {
        "test_name": "FrontCamera",
        "test_type": "Mandatory",
        "visibility": "true",
        "report_name": "FrontCamera",
        "test_category": "manual",
        "order": "43"
      },
      "Flash": {
        "test_name": "Flash",
        "test_type": "Mandatory",
        "visibility": "true",
        "report_name": "Flash",
        "test_category": "manual",
        "order": "1"
      },
      "AutoFocus": {
        "test_name": "AutoFocus",
        "test_type": "Mandatory",
        "visibility": "true",
        "report_name": "AutoFocus",
        "test_category": "manual",
        "order": "35"
      },
      "VideoRecord": {
        "test_name": "VideoRecord",
        "test_type": "mandatory",
        "visibility": "true",
        "report_name": "VideoRecord",
        "test_category": "manual",
        "order": "10"
      },
      "FrontVideoRecorder": {
        "test_name": "FrontVideoRecorder",
        "test_type": "mandatory",
        "visibility": "true",
        "report_name": "FrontVideoRecorder",
        "test_category": "manual",
        "order": "11"
      }
    },
    "screen_package": {
      "Screen": {
        "test_name": "Screen",
        "test_type": "Mandatory",
        "visibility": "true",
        "report_name": "Screen",
        "test_category": "manual",
        "order": "21"
      }
    }
  }
}

Following is the java code :

public void parseJSON(String jsonName, String packageName) {
        try {
            JSONObject jsonRootObject = new JSONObject(jsonName);
            if (jsonRootObject != null && jsonRootObject.length() > 0) {
                Iterator<String> packageKeys = jsonRootObject.keys();
                if (packageKeys != null) {
                    while(packageKeys.hasNext()){
                        String currentPackageKey = packageKeys.next();
                        if(currentPackageKey!= null && currentPackageKey.length()>0 &&
                                currentPackageKey.equals(packageName)){
                            JSONObject jsonPackageObject = new JSONObject(currentPackageKey);  //line 150
                            if(jsonPackageObject!=null && jsonPackageObject.length()>0) {
                                Iterator<String> testNameKeys = jsonPackageObject.keys();
                                if(testNameKeys!=null){
                                    while(testNameKeys.hasNext()) {
                                        String currentTestNameKey = testNameKeys.next();
                                        if(currentTestNameKey!=null && currentTestNameKey.length()>0) {
                                            //do something
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("Exception caught : "+e.getLocalizedMessage());
        }
    }

Solution

  • JSONException indicates that some exception happened during JSON processing.

    You should use getJSONObject(String name).

    JSONObject getJSONObject (String name)
    

    Returns the value mapped by name if it exists and is a JSONObject, or throws otherwise

    Code Structure

     JSONObject screen_package = jsonobject.getJSONObject("screen_package");