0

I am trying to parse a JSON response. I am getting JSON response like below:

"libraryLastModified" : "2012-10-10 03:57:26",
"playlists" : { "10063" : { "id" : "10063",
       "name" : "Favorites",
       "songs" : [ "10006134",
           "10006053",
           "10006274",
           "10006167",
        ]
    },
    "10157" : { "id" : "10157",
        "name" : "80s",
        "songs" : [ "10006694",
            "10006695",
            "10006697",
            "10006699",
            "10006698",
        ]
    }

How can I access the id & name values?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
user1737884
  • 8,137
  • 4
  • 15
  • 15

3 Answers3

3

I love GSON. In this scenario you'd create two classes.

public class PLayList {
 private int id;
 private String name;
 private List<Integer> songs;
 //getters and setters
}

public class Library {
 private Date libraryLastModified;
 private List<Playlist> playlists;
 //getters and setters
}

Then you can write

 Gson gson = new Gson();
 Library result = gson.fromJson(theInput, Library.class);

Since the playlists is coming to you as key:value you'd need to write a custom deserializer for them. Taje a look at GSON deserializing key-value to custom object for how to do that

Community
  • 1
  • 1
Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
1

In pseudocode. I don't remember exactly the JSON methods

JSONObject mainObj = parseJson
JSONObject playLists = mainObj.getJSONObject("playlists")
JSONObject myList = playList.getJSONObject("10063")

id = myList.getString("id")

To iterate over several lists, you'd better transform playlists to a JSONArray, then you can iterate over it. If you can't do that, check the Android JSON API and check how to get all keys for a JSONObject and then iterate over the keys

for(int i=0;i<playlistKeys.length;i++){
  playlistObj = playLists.getJSONObject(playlistsKey[i])
}
Maragues
  • 37,861
  • 14
  • 95
  • 96
  • This works but how to make it iterate as i am having multiple playlist 10063, 10064 .... – user1737884 Oct 11 '12 at 11:42
  • investigate for yourself, I've pointed you in the right direction, but i'm not your personal army. Google for "android jsonobject" and read the api specification – Maragues Oct 11 '12 at 12:55
0

use google Gson. http://code.google.com/p/google-gson/

class Response{
Date libraryLastModified;
Playlist []playlists;

class Playlist{
    Long id;
    String name;
    Long[] songs;
}

}
String _response=... //Your response from web
Response response = new GsonBuilder().setDateFormat("yyyy-MM-dd' 'HH:mm:ss").create().fromJson(_response, Response.class);

String songName = response.playlists[0].name;
Artem Zelinskiy
  • 2,201
  • 16
  • 19
  • its Throwin Exception ' 10-11 12:51:08.615: W/System.err(359): com.google.gson.JsonSyntaxException: java.text.ParseException: Unparseable date: 2012-10-10 03:57:26 at com.google.gson.DefaultTypeAdapters$DefaultJavaSqlDateTypeAdapter.deserialize(DefaultTypeAdapters.java:416) ' – user1737884 Oct 11 '12 at 12:57