2014-10-10 81 views
-1

我需要你的幫助來解析嵌套的JSON對象。嵌套的JSON對象解析在android中使用volley

附JSON數據:

{ 
    "31": { 
    "basic": { 
     "node_id": "31", 
     "title": "test", 
     "alias": "test", 
     "description": "test", 
     "site_id": "151336557", 
     "node_type": "7", 
     "privacy": "7", 
     "deleted": "0", 
     "status": "1", 
     "created_date": "1379169518", 
     "updated_date": "1379169518", 
     "created_by": "140513626519686828", 
     "updated_by": null, 
     "readable_date": "14th Sep, 2013" 
    }, 
    "meta": { 
     "forum_id": "61" 
    }, 
    "comments": { 
     "count": 1 
    }, 
    "likes": { 
     "count": 0 
    }, 
    "tags": [], 
    "node_id": "31" 
    }, 
    "32": { 
    "basic": { 
     "node_id": "32", 
     "title": "testing discussion", 
     "alias": "testing-discussion", 
     "description": "testing", 
     "site_id": "151336557", 
     "node_type": "7", 
     "privacy": "7", 
     "deleted": "0", 
     "status": "1", 
     "created_date": "1379493816", 
     "updated_date": "1379493816", 
     "created_by": "140513795022034166", 
     "updated_by": null, 
     "readable_date": "18th Sep, 2013" 
    }, 
    "meta": { 
     "forum_id": "65" 
    }, 
    "comments": { 
     "count": 1 
    }, 
    "likes": { 
     "count": 0 
    }, 
    "tags": [], 
    "node_id": "32" 
    } 
} 

附加Java代碼:

private void makeJsonObjectRequest() { 
    showpDialog(); 

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,   urlJsonObj, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      Log.d(TAG, response.toString()); 

      JSONObject phone = response.getJSONObject("31").getJSONObject("basic"); 
      String name = phone.getString("title"); 
      String email = phone.getString("description"); 
      JSONObject comments = response.getJSONObject("31").getJSONObject("comments"); 
      String home = comments.getString("count"); 
      JSONObject like = response.getJSONObject("31").getJSONObject("likes"); 
      String mobile = like.getString("count"); 

      jsonResponse = ""; 
      jsonResponse += "Name: " + name + "\n\n"; 
      jsonResponse += "Email: " + email + "\n\n"; 
      jsonResponse += "Home: " + home + "\n\n"; 
      jsonResponse += "Mobile: " + mobile + "\n\n\n"; 

      txtResponse.setText(jsonResponse); 

我需要檢索所有的對象,但我在這裏只獲取一個對象(我的意思是所有node_id S) 。我需要你的建議。 謝謝。

這一部分
+0

使用本網站http://www.jsonschema2pojo.org/,它可以幫助你從Json的轉換爲對象 – 2014-10-10 11:45:22

回答

0

你得到與關鍵"31"response.getJSONObject("31"))的條目,但是,你應該重複所有鍵:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() { 
    @Override 
    public void onResponse(JSONObject response) { 
     for (String key : response.keySet()) { 
      JSONObject entry = response.getJSONObject(key); 
      Log.d(TAG, entry.toString()); 

      JSONObject phone = entry.getJSONObject("basic"); 
      String name = phone.getString("title"); 
      String email = phone.getString("description"); 
      JSONObject comments = entry.getJSONObject("comments"); 
      String home = comments.getString("count"); 
      JSONObject like = entry.getJSONObject("likes"); 
      String mobile = like.getString("count"); 

      jsonResponse = ""; 
      jsonResponse += "Name: " + name + "\n\n"; 
      jsonResponse += "Email: " + email + "\n\n"; 
      jsonResponse += "Home: " + home + "\n\n"; 
      jsonResponse += "Mobile: " + mobile + "\n\n\n"; 

      txtResponse.setText(txtResponse.getText() + "\n\n" + jsonResponse); //get the old text and add it to it 
     } 
    } 
} 

編輯:你說你只想要node_id的?這也是有可能的:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() { 
    @Override 
    public void onResponse(JSONObject response) { 
     List<String> nodeIds = new ArrayList<String>(); 
     for (String key : response.keySet()) { 
      JSONObject entry = response.getJSONObject(key); 
      nodeIds.add(enty.getJSONObject("basic").getString("node_id")); 
     } 
     txtResponse.setText(ListUtils.toString(nodeIds)); //from apaches commons library 
    } 
} 
+0

感謝烏拉圭回合的答覆。我試過你的代碼,但我得到這個錯誤「只能遍歷一個數組或java.lang.Iterable的實例」爲response.keys()。「你能建議嗎?」 – Srikanth 2014-10-10 12:50:44

+0

@srikanth使'keys()''keySet )' – stealthjong 2014-10-10 12:57:47

+0

我得到無法解析方法keySet() – gogu 2015-05-24 05:21:05

0

response.getJSONObject("31") 

您需要更改31一個變量,叫給你的對象的每個領域