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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

This works but how to make it iterate as i am having multiple playlist 10063, 10064 ....
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
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;

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.