jsonobject无法转换为jsonarray嵌套的jsonarray android

时间:2013-10-22 08:26:45

标签: android

我想解析一个嵌套的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");

我收到jsonobject cannot be converted to jsonarray错误 dialogs = d.getJSONArray("dialogdetails");

我的json数组

{"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":"icube1solutions@gmail.com","d_category_id":"2","d_category_name":"Games"}}]}   

2 个答案:

答案 0 :(得分:0)

{ // json object
    "dialog_details": [ // json array
        {
            "msg": "success", 
            "dialogdetails": { // json object
              "dialog_id": "139",            

dialogdetails不是JSONArray,而是JSONObject

更改为JSONObject

dialogs = d.getJSONArray("dialogdetails");

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

[表示json数组节点

{表示json对象节点

    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");   
    }

答案 1 :(得分:0)

dialogdetails 不是JsonAray ..

你在这里做错了

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

它是一个JsonObject

 dialogs = d.getJSONObject("dialogdetails");

JSON字符串以大括号开头{代表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": "icube1solutions@gmail.com",
                "d_category_id": "2",
                "d_category_name": "Games"
            }