0

I am trying to parse the following JSON in Java program.

JSON

{
    "data": {
        "pageIDentifier":" nametitle",
        "page": {
            "pageID”:” sports_league_member",
            "platform":" www",
            "activityType":" ent",
            "businessUnit":" ent",
            "productLOB":" ent",
            "productOffered":" abc",
            "productQualifier”:” abc:a bc”,
            "flowType”:” sports_com”,
            "pageDesc”:” desc”,
            "attributes": {
                "pageType":" www",
                "host”:” finaluser”,
                "appId”:” SportsAppID_user”,
                "daEnvironment”:” releaser”,
                "jvm":" ent_logon_01",
                "xCKey":" SportsAppID_user",
                "daUID":" jddc9yu5pi1yy6",
                "sysEnv”:” user”,
                "uri”:” /www/sportscenter”,
                "daPageName":" "
            }
        }
    }
}

JAVA Program

URL url = getClass().getResource("test.json");
File file = new File(url.getPath());
String jsonData = readFile(file.getAbsolutePath());
JSONObject jobj = new JSONObject(jsonData);
JSONArray jarr = new JSONArray(jobj.getJSONArray("data").toString());
System.out.println("jarr: " + jarr);

I am getting the following error when executing this code. Please note, jsonData is giving the entire json string without any issue.

org.json.JSONException: JSONObject["data"] is not a JSONArray.

How can i parse "data" value from the above json value? Please advise.

5
  • 2
    your JSON example clearly shows that data is not an array! Commented Mar 28, 2018 at 21:54
  • @JonnathanQ is right, data it not an array, if it is an array you should read it by jobj.getJSONArray("data") without doing new JSONArray Commented Mar 28, 2018 at 21:58
  • Also you have some not regular double quotes but 'inclined' ones Commented Mar 28, 2018 at 22:27
  • @JoseDaSilva: that's probably an artifact of using some MS program(s) while posting -- MS likes to change quote marks and calls this 'smart quotes'. If the real data had those invalid chars JSONObject would barf before reaching the .getJSONArray call. Commented Mar 28, 2018 at 22:52
  • JSON arrays begin with [ and end with ]. For example [1,2,3,4]. The value associated with data is a JSON object since it begins with { and ends with }. See json.org Commented Mar 29, 2018 at 5:07

1 Answer 1

2

data is not an array

Try this :

System.out.println("jarr: " + jobj.getJSONObject("data").toString());
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.