1

I have the following JSON in my android application and I'm using Springs for android RestTemplate. Is it somehow possible to fetch only internal list from this json ? Currently I have to create a wrapper object and then I can fetch List<Cast> casts; from it - it's a bit inconvenient.

{
  "id": 550,
  "cast": [
    {
      "cast_id": 4,
      "character": "The Narrator",
      "credit_id": "52fe4250c3a36847f80149f3",
      "id": 819,
      "name": "Edward Norton",
      "order": 0,
      "profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
    }
  ]
}
ashur
  • 4,177
  • 14
  • 53
  • 85

3 Answers3

0

I know two solutions to solve this :

  1. What you've done : Wrapping the list using a class
  2. Write you're own JSON Deserializer, take a look here http://wiki.fasterxml.com/JacksonHowToCustomDeserializers

But I think the ultimate solution is in this post: Spring/json: Convert a typed collection like List<MyPojo>

Community
  • 1
  • 1
0

If Cast is a POJO you could try use the Jackson ObjectMapper class like this

ObjectMapper mapper = new ObjectMapper();
String jsonStr = ...
List<Cast> casts = mapper.readValue(jsonStr, new TypeReference<List<Cast>>(){});
Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
0

You can grab the field, cast, and convert it like so:

    final String json = "{\n" +
            "  \"id\": 550,\n" +
            "  \"cast\": [\n" +
            "    {\n" +
            "      \"cast_id\": 4,\n" +
            "      \"character\": \"The Narrator\",\n" +
            "      \"credit_id\": \"52fe4250c3a36847f80149f3\",\n" +
            "      \"id\": 819,\n" +
            "      \"name\": \"Edward Norton\",\n" +
            "      \"order\": 0,\n" +
            "      \"profile_path\": \"/iUiePUAQKN4GY6jorH9m23cbVli.jpg\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    final List<Cast> casts;
    try {
        final JsonNode cast = objectMapper.readTree(json).get("cast");
        casts = objectMapper.convertValue(cast, new TypeReference<List<Cast>>() {});
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
alexwen
  • 1,128
  • 7
  • 16