2014-10-10 2 views
-1

Мне нужна ваша помощь при разборе вложенного объекта JSON.Вложенный анализ объектов JSON в андроиде с использованием 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 с) , Мне нужны ваши предложения. Спасибо.

+0

Используйте этот сайт http://www.jsonschema2pojo.org/, это поможет вам в преобразовании из Json в объект –

ответ

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 
     } 
    } 
} 

EDIT: вы утверждаете, вы хотите только node_id «S? Это тоже возможно:

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

+0

@srikanth Сделать' keys() '' keySet () ' – stealthjong

+0

i get не может решить метод keySet() – gogu

0

на этой части

response.getJSONObject("31") 

вам нужно изменить 31 для переменного и называют это каждое поле вашего объекта

Смежные вопросы