0

Java object:

public class Foo {
 @JsonProperty("name")
 private String name;
 @JsonProperty("surname")
 private String surname;

 // getters + setters
}

JSON:

   {  
   "meta":{  
      "code":200
   },
   "data":[  
      {  
         "name":"John",
         "surname":"Smith"
      }
   ]
}

API call:

return restTemplate.getForEntity(requestUrl, Foo[].class).getBody();

Is it possible to parse "data" array without creating an additional wrapper class? I tried adding the @JsonRootName("data") annotation on top of my Java class, but it did not work.

Bravo
  • 1,944
  • 4
  • 29
  • 53
  • Gson can do this fairly easily. http://stackoverflow.com/questions/37231894/using-gson-and-retrofit-2-to-deserialize-complex-api-responses – OneCricketeer Feb 19 '17 at 16:01
  • that looks even more complex - adding hundreds of classes for parsing is not what I am looking for – Bravo Feb 19 '17 at 16:05
  • Hundreds? It's 1 TypeAdapterFactory and Jackson has custom deserialization itself. Java is object oriented, so what do you have against classes? – OneCricketeer Feb 19 '17 at 19:09

1 Answers1

0

You can try with:

import org.json.*;

JSONObject obj = new JSONObject(" .... ");
String name = obj.getJSONObject("data").getString("name");
juanlumn
  • 6,155
  • 2
  • 30
  • 39