-1

我想解析一个嵌套的 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"}}]}   
4

2 回答 2

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");   
    }
于 2013-10-22T08:29:35.527 回答
0

dialogdetails不是 JsonAray ..

你在这里做错了

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

它是一个 JsonObject

 dialogs = d.getJSONObject("dialogdetails");

JSON String 以大括号开头 { 代表 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"
            }
于 2013-10-22T08:30:32.760 回答