Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to parse a nested jsonarray

JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            dialogcreated = json.getJSONArray(TAG_DETAILS);

            // Log.d("apptoken",login.toString());
            for (int i = 0; i < dialogcreated.length(); i++) {
                JSONObject d = dialogcreated.getJSONObject(i);

                String msg = d.getString(TAG_MSG);
                dialogs = d.getJSONArray("dialogdetails");

I am getting jsonobject cannot be converted to jsonarray error on dialogs = d.getJSONArray("dialogdetails"); line

my json array

{"dialog_details":[{"msg":"success","dialogdetails":{"dialog_id":"139","dialog_category":"2","dialog_title":"apptesting1","dialog_description":"apptesting2","dialog_image":null,"dialog_type":"P","mem_id":"27","temp_moderator_assigned":"0","moderator_assigned":"0","created_on":"6 minutes ago","updated_by":null,"updated_on":"6 minutes ago","mem_dialog_type":"P","published":"0","dialog_status":"1","dialog_archived":"0","dialog_votes":"0","featured":"0","dialog_members":"0","dialog_posts":"0","wtavg":"0","d_member_username":"icube2","d_mem_id":"27","d_member_avatar":"0","d_member_email":"[email protected]","d_category_id":"2","d_category_name":"Games"}}]}   
share|improve this question
    
dialogdetails is an an JsonObject not a JsonArray –  Nezam Oct 22 '13 at 8:29
1  
dialogdetails is a JSONObject. [ represent JSONArray, { represent JSONObject –  Tom Hart Oct 22 '13 at 8:29
    
@AvinashKumarPankaj may be he's not your bro.Rather he's a "she".Lol.(Light moment) –  Nezam Oct 22 '13 at 8:40
add comment

2 Answers

up vote 0 down vote accepted
{ // json object
    "dialog_details": [ // json array
        {
            "msg": "success", 
            "dialogdetails": { // json object
              "dialog_id": "139",            

dialogdetails is not a JSONArray its a JSONObject

Change to JSONObject

dialogs = d.getJSONArray("dialogdetails");

to

JSONObject dialogs = d.getJSONObject("dialogdetails");

[ represents json array node

{ represetns json object node

    JSONObject jb = new JSONObject("myjsonstring");
    JSONArray jr = (JSONArray)jb.getJSONArray("dialog_details"):
    for(int i=0;i<jr.length();i++)
    {
       JSONObject d = (JSONObject) jr.getJSONObject(i);
       String msg = d.getString(TAG_MSG);
       JSONObject dialogs = d.getJSONObject("dialogdetails");
       String dialog_id= dialogs.getString("dialog_id");   
    }
share|improve this answer
add comment

dialogdetails is not JsonAray..

And you are doing wrong here

 dialogs = d.getJSONArray("dialogdetails");//Wrong

Its a JsonObject

 dialogs = d.getJSONObject("dialogdetails");

JSON String start with braces { stands for JsonObject

"dialogdetails": {
                "dialog_id": "139",
                "dialog_category": "2",
                "dialog_title": "apptesting1",
                "dialog_description": "apptesting2",
                "dialog_image": null,
                "dialog_type": "P",
                "mem_id": "27",
                "temp_moderator_assigned": "0",
                "moderator_assigned": "0",
                "created_on": "6 minutes ago",
                "updated_by": null,
                "updated_on": "6 minutes ago",
                "mem_dialog_type": "P",
                "published": "0",
                "dialog_status": "1",
                "dialog_archived": "0",
                "dialog_votes": "0",
                "featured": "0",
                "dialog_members": "0",
                "dialog_posts": "0",
                "wtavg": "0",
                "d_member_username": "icube2",
                "d_mem_id": "27",
                "d_member_avatar": "0",
                "d_member_email": "[email protected]",
                "d_category_id": "2",
                "d_category_name": "Games"
            }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.