0

my problem is that my android app does not parse the following JSON data. If I use other json sources it parses OK. I've validated the json bellow and there were no problems. It doesn't throw any error. I can't understand why. Could it be because of the date being first?

Json data looks like this:

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

And my code for parsing:

       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();


            }
        });
gogu
  • 613
  • 1
  • 9
  • 19

3 Answers3

1

The reason your code does not work is, your response i.e.

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

is having a JSONObject called "2015-05-23 18:48:58"

after you call

JSONObject obj = response.getJSONObject(i);

again make another JSONObject like

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

now do your thing

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

Also, just in case you have to get the key values (like "2015-05-23 18:48:58".. as you say there are 30 of them) read java-iterate-over-jsonobject.

Something like the following code can solve the problem

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 ) {

    }
}
Community
  • 1
  • 1
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • this is just the first data, the json has almost 30 entries – gogu May 23 '15 at 18:27
  • still nothing showsup, i have also edited my json data, the first was only one element of many. Now i inserted another element so it would be more explanatory. I have alsmost 30 entries like that, with different dates – gogu May 23 '15 at 18:51
0

This json data is wrong !! The correct one is :

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

And in this case to correct way to retrieve data is :

JSONObject parentObj = response.getJSONObject(i);
JSONObject obj= parentObj .getJSONObject("2015-05-23 18:48:58");
Fouad Wahabi
  • 804
  • 7
  • 16
0

Can't get it to work in volley with iterator, so i am trying this solution, but it gives me Cannot resolve method 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();
                }
            }
gogu
  • 613
  • 1
  • 9
  • 19