android volley json解析器问题

时间:2015-05-23 18:18:26

标签: java android json

我的问题是我的Android应用程序无法解析以下JSON数据。如果我使用其他json来源,则会解析好。我已经验证了json bellow并且没有问题。它不会抛出任何错误。我不明白为什么。可能是因为第一个日期?

Json数据如下所示:

  {
  "2015-05-23 18:48:58": {
    "Titlu": "kgjsdfklgjfdgjsdlgjdgjfd",
    "PozaPrincipala": "27602",
    "Descriere": "fkdsgjslkfdglkgfdsgfdklnm",
    "CMSdate": "2015-05-23 18:48:58",
    "url": "http://fsdgdgfdggsdfgfgfdg",
    "thumb": "http://dasidsaionofafnoinfasnisa"
  },
  "2015-05-21 20:17:36": {
    "Titlu": "jhsdkgjshfgsjdfkhgsf",
    "PozaPrincipala": "27592",
    "Descriere": "kldsjgfhgdhgfhgsdfhifhgisf",
    "CMSdate": "2015-05-21 20:17:36",
    "url": "http://gsfdgfsdgsfdgfdgfdg",
    "thumb": "http://dvsddggsfngfsgsfn"
  }
}

我的解析代码:

       private static final String url = "http://xxx.ro/xxx";
// Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {

                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();
                        Log.d(TAG, response.toString());
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("Titlu"));
                                movie.setThumbnailUrl(obj.getString("thumb"));

                                movie.setLink(obj.getString("url"));
                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();

                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {

            public void onErrorResponse(VolleyError error) {

                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();


            }
        });

3 个答案:

答案 0 :(得分:1)

您的代码不起作用的原因是您的回复,即

{
    "2015-05-23 18:48:58": {
        "Titlu": "This is the title",
        "Descriere": "Description",
        "CMStags": "tags",
        "CMSdate": "2015-05-23 18:48:58",
        "url": "http:\/\/www.xxx.ro/jdshafhdafhkas",
        "thumb": "http:\/\/img.xxx.ro\/?u=http%3A%2F%2Fst.xxx.ro%2Fcms_websites%2Fcms_something%2Flibrary%2Fimg%2F2015%2F05%2Fsome_thumb.jpg&amp;w=300&amp;h=215&c=1"
    }
}

JSONObject名为"2015-05-23 18:48:58"

打电话后

JSONObject obj = response.getJSONObject(i);

再次制作另一个JSONObject

JSONObject objItem = obj.getJSONObject("2015-05-23 18:48:58");

现在做你的事

Movie movie = new Movie();
movie.setTitle(objItem.getString("Titlu"));
...
...
  

此外,如果您必须获取关键值(例如"2015-05-23 18:48:58" ..正如您所说的那样有30个),请阅读java-iterate-over-jsonobject

类似下面的代码可以解决问题

jObject = new JSONObject(contents.trim());
Iterator<?> keys = jObject.keys();

while( keys.hasNext() ) {
    String key = (String)keys.next();
    // save this key in a ArrayList of String for 
    // passing to JSONObject objItem = obj.getJSONObject(key);
    // instead of JSONObject objItem = obj.getJSONObject("2015-05-23 18:48:58");
    if ( jObject.get(key) instanceof JSONObject ) {

    }
}

答案 1 :(得分:0)

这个json数据错了!! 正确的是:

{
    "2015-05-23 18:48:58": {
    "Titlu": "This is the title",
    "Descriere": "Description",
    "CMStags": "tags",
    "CMSdate": "2015-05-23 18:48:58",
    "url": "http:\/\/www.xxx.ro/jdshafhdafhkas",
    "thumb": "http:\/\/img.xxx.ro\/?u=http%3A%2F%2Fst.xxx.ro%2Fcms_websites%2Fcms_something%2Flibrary%2Fimg%2F2015%2F05%2Fsome_thumb.jpg&amp;w=300&amp;h=215&c=1"
    } // you need to end-up the object
}

在这种情况下,纠正检索数据的方法是:

JSONObject parentObj = response.getJSONObject(i);
JSONObject obj= parentObj .getJSONObject("2015-05-23 18:48:58");

答案 2 :(得分:0)

无法使用迭代器使其在凌空中工作,所以我正在尝试此解决方案,但它让我无法解析方法keySet():

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            for (String key : response.keySet()) {

                        try {
                            JSONObject obj = response.getJSONObject(key);
                            Log.d(TAG, obj.toString());


                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("Titlu"));
                            movie.setThumbnailUrl(obj.getString("thumb"));

                            movie.setLink(obj.getString("url"));
                            // adding movie to movies array
                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();

                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }