2

Here is my code snippet:

    jArray = jChild.getJSONArray("users");
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject jObject = (JSONObject) jArray.get(i);    //Exception thrown in this line

}

Edit:

Logcat:

09-30 09:06:23.404: E/AndroidRuntime(4011): java.lang.RuntimeException: An error occured while executing doInBackground()
09-30 09:06:23.404: E/AndroidRuntime(4011):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-30 09:06:23.404: E/AndroidRuntime(4011): Caused by: java.lang.ClassCastException: java.lang.String
09-30 09:06:23.404: E/AndroidRuntime(4011):     at com.example.DbAccess.loadMonthView(DbAccess.java:196)

This is my jsonArray:

 [{"uid":"6","status":"absent","name":"xyz"},{"uid":"7","status":"absent","name":"abc Paul"}]

I want to retrieve JSONObject from JSONArray.

5
  • 2
    can you shows us your json Commented Sep 30, 2013 at 3:46
  • Sure..just check the edit. Commented Sep 30, 2013 at 3:53
  • 1
    Post the Logcat for exception also Commented Sep 30, 2013 at 3:54
  • @user2749218 what is jChild Commented Sep 30, 2013 at 4:01
  • I hope this is not your full JSON, is it? Because there is no 'key' as 'users' to execute getJSONArray("users"). Commented Sep 30, 2013 at 4:07

2 Answers 2

2
[ // represents json array node
    {     // represents json obeject node  
        "uid": "6",
        "status": "absent",
        "name": "xyz"
    },
    {
        "uid": "7",
        "status": "absent",
        "name": "abc Paul"
    }
]

What you have is a JSONArray. You need to parse the values and get the string.

     JSONArray jr = new JSONArray("myjsonstring");
     for(int i=0;i<jr.length();j++)
     {
     JSONOBject jb = jr.getJSONObject(i);
     String uid = jb.getString("uid");
     String status = jb.getString("status");
     String name = jb.getString("name");  
     } 
Sign up to request clarification or add additional context in comments.

1 Comment

@user2749218 you also had this JSONObject jObject = (JSONObject) jArray.get(i); so the problem should be probably here jArray = jChild.getJSONArray("users"). Glad it helped
0

Your Json file has an array at the top level. When the JSonParser parses it, it's returning it as a JSONArray. You're trying to cast it to a JSONObject instead (which is like a map, or dictionary.) What you need to do is this:

Object obj;
try {

    obj = parser.parse(sCurrentLine);
    JSONArray jsonArray = (JSONArray) obj;
    for(obj : jsonArray){//not sure of the exact syntax, I don't have an IDE in front of me.
        JSONObject jsonObject = (JSONObject)obj;
        JSONObject realTitle = (JSONObject)jsonObject.get("0");
        String name = (String) realTitle.get("title");
    }


} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

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.