The JSON response that you get from the provider is a serialized representation of the hierarchy of different objects, but from your description it sounds as if you really need to use and work with a specific subset of this representation, a collection of artists.
One solution to mirroring this view is to create the same hierarchy of Java classes that creates additional overhead in the form of unnecessary classes. From what I understand, this is what you want to avoid.
The org.json project created a common JSONObject class, which is a single common key / value pair in a larger JSON view, JSONObject can contain other JSONObjects and JSONArrays , mirroring the view without additional overhead for servicing and writing additional classes.
Thus, these two objects can be reused at many levels of the hierarchy in the JSON representation, without requiring replication of the structure. Here is an example of how you can continue:
// jsonText is the string representation of your JSON JSONObject jsonObjectWrapper = new JSONObject(jsonText); // get the "artists" object JSONObject jsonArtists = jsonObjectWrapper.get("artists"); // get the array and pass it to Jackson ObjectMapper, using TypeReference // to deserialize the JSON ArrayList to a Java ArrayList. List<Artist> artists = objectMapper.readValue( jsonObjectWrapper.getString("artist"), new TypeReference<ArrayList<Artist>>() { });
Using the method described above, you will choose the additional overhead associated with the need to write additional layers of POJO objects that do nothing but add unnecessary clutter.
TestCollectionDeserialization contains some examples of the readValue method when working with collections and may be useful.
jmort253
source share