-3

I'm new to JSON and REST. I'm working with Testing a rest API that returns strings like these:

[{
    "Supervisor_UniqueName": "adavis",
    "Active": "true",
    "DefaultCurrency_UniqueName": "USD",
    "arches_type": "x-zensar/primary",
    "Groups": "",
    "TimeZoneID": "US/Pacific-New",
    "UniqueName": "!!pl-10958-611879240",
    "EmailAddress": "!!pl-10958-611879240@devmail.zensar.com",
    "LocaleID_UniqueName": "en_US",
    "Name": "!!pl-10958-611879240"
}, {
    "Supervisor_UniqueName": "adavis",
    "Active": "true",
    "DefaultCurrency_UniqueName": "USD",
    "arches_type": "x-zensar/primary",
    "Groups": "",
    "TimeZoneID": "US/Pacific-New",
    "UniqueName": "!!pl-10958-789764779",
    "EmailAddress": "!!pl-10958-789764779@devmail.zensar.com",
    "LocaleID_UniqueName": "en_US",
    "Name": "!!pl-10958-789764779"
}, {
    "Supervisor_UniqueName": "adavis",
    "Active": "true",
    "DefaultCurrency_UniqueName": "USD",
    "arches_type": "x-zensar/primary",
    "Groups": "Report User",
    "TimeZoneID": "US/Pacific-New",
    "UniqueName": "105838945",
    "EmailAddress": "105838945@devmail.zensar.com",
    "LocaleID_UniqueName": "en_US",
    "Name": "105838945"
}, {
    "Supervisor_UniqueName": "adavis",
    "Active": "true",
    "DefaultCurrency_UniqueName": "USD",
    "arches_type": "x-zensar/primary",
    "Groups": "Report User, Report Manager, Report Administrator",
    "TimeZoneID": "US/Pacific-New",
    "UniqueName": "112352755",
    "EmailAddress": "112352755@devmail.zensar.com",
    "LocaleID_UniqueName": "en_US",
    "Name": "112352755"
}]

I invoke rest API as below:

final ResponseEntity<String> responseEntity = restTemplate.getForEntity(resourceUrl , String.class);

where restTemplate is of type RestTemplate

I want to convert them into a JSON array, access each Json Object and get the Field and Values for each.

Is it possible to do it using jackson ?

stefanhorne
  • 1,619
  • 3
  • 16
  • 23
Sajeev
  • 73
  • 1
  • 1
  • 5
  • What have you tried, what did you get, what do you expect, and what errors are you running into? – ssube May 26 '16 at 14:15
  • Sorry .. I did more research and this post solved my problem http://stackoverflow.com/questions/5983932/converting-from-jsonarray-to-string-then-back-again – Sajeev May 29 '16 at 01:27

1 Answers1

0

You could use Jackson.

This is (part of) your JSON:

[  
   {  
      "Supervisor_UniqueName":"adavis",
      "Active":"true",
      "DefaultCurrency_UniqueName":"USD",
      "arches_type":"x-zensar/primary",
      "Groups":"",
      "TimeZoneID":"US/Pacific-New",
      "UniqueName":"!!pl-10958-611879240",
      "EmailAddress":"!!pl-10958-611879240@devmail.zensar.com",
      "LocaleID_UniqueName":"en_US",
      "Name":"!!pl-10958-611879240"
   },
   {  
      "Supervisor_UniqueName":"adavis",
      "Active":"true",
      "DefaultCurrency_UniqueName":"USD",
      "arches_type":"x-zensar/primary",
      "Groups":"",
      "TimeZoneID":"US/Pacific-New",
      "UniqueName":"!!pl-10958-789764779",
      "EmailAddress":"!!pl-10958-789764779@devmail.zensar.com",
      "LocaleID_UniqueName":"en_US",
      "Name":"!!pl-10958-789764779"
   }
]

Just to give you an idea:

public MyClass {

  @JsonProperty("Supervisor_UniqueName")
  private String superVisorUniqueName;

  @JsonProperty("Active")
  private String active;

  // etc

  // getters and setters    
}

Map JSON to your MyClass:

ObjectMapper mapper = new ObjectMapper();
List<MyClass> obj = mapper.readValue(json, new TypeReference<List<MyClass>>() {});
Diyarbakir
  • 1,999
  • 18
  • 36
  • Thanks so much for responding. I am afraid I don't want to write the mapper class simply because it's not manageable. Right now using the API I got users data, Like this the rest API will return different types of master data (Suppliers, Products, CommodityCodes , Countries etc). I am afraid I can not write POJO's for each . It's a overkill. what do you think ? – Sajeev May 26 '16 at 14:35