【问题标题】:How to parse nested json obiect如何解析嵌套的json对象
【发布时间】:2013-04-17 13:47:28
【问题描述】:

我有这个 json:

{"data": [{"name" "category" "id" "picture":{"data":{"url":}}}]

我的困难是如何解析图片字段。我试过这段代码:

  public class JSONParser {
        public List<HashMap<String,Object>> parse(JSONObject jObject){      
            JSONArray jGames = null;
                try {       
                    jGames = jObject.getJSONArray("data");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return getGames(jGames);
            }       
            private List<HashMap<String, Object>> getGames(JSONArray jGames){
                int gameCount = jGames.length();
                List<HashMap<String, Object>> gameList = new ArrayList<HashMap<String,Object>>();
                HashMap<String, Object> game = null;    
                for(int i=0; i<gameCount;i++){
                    try {
                        game = getGame((JSONObject)jGames.get(i));
                        gameList.add(game);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                return gameList;
            }
            private HashMap<String, Object> getGame(JSONObject jGame){
                HashMap<String, Object> game = new HashMap<String, Object>();
                String gameName = "";
                String logo="";
                String user = "";           
                try {
                    gameName = jGame.getString("name");
                    logo = jGame.getString("logo_url");
                    user = jGame.getString("daily_active_users");
                    String details ="Utenti attivi : " + user + "\n";
                    game.put("name", gameName);
                    game.put("logo_url", R.drawable.icon);
                    game.put("logo_path", logo);
                    game.put("details", details);
                } catch (JSONException e) {         
                    e.printStackTrace();
                }       
                return game;
            }
        }

【问题讨论】:

  • 错误/问题是?
  • 以上发布的json无效..签入jsonlint.com

标签: android json parsing object nested


【解决方案1】:

本教程将帮助您Android JSON Parsing Tutorial

【讨论】:

  • 我试过:for(int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2020-05-18
  • 2019-12-18
  • 2019-08-28
相关资源
最近更新 更多