1

I have a JSON array of objects in a String variable:

String response = target.path("flight").path("lufthansa").request().get(String.class);

It looks like below:

[
  {
    "flightNo": "AA65",
    "flightName": "Lufthansa Airlines",
    "departingFrom": "Beruit",
    "arrivingAt": "Heathrow Airport",
    "departureTime": "10-11-2015",
    "arrivalTime": "10-11-2015",
    "cost": 45000.0
  }
]        

How can I parse it to a List<FlightBean>?

List<FlightBean> flightInfo = new ArrayList<FlightBean>();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joseph
  • 85
  • 11
  • Browsers are completely unaware of anything like JSF and/or JAX-RS. What problem are you experiencing while sending a response received from a service? I would prefer a service class to receive a response from a (web) service so that it can then be consumed by JSF in a way that normally JSF uses to communicate with a service class. – Tiny Nov 13 '15 at 18:31
  • @Tiny, Thanks. That's right. I did all that. i just showed a snippet where the challenge is for me. How do i parse the reponse am getting at the console into form that i can display on the browser. I want to display it in some tabular form – Joseph Nov 13 '15 at 18:45
  • Parsing a JSON response is the job of an appropriate object mapper like Jackson, Google Gson. Once you map the response to an appropriate object type like a collection / map / array, it can be used in a normal Java way. – Tiny Nov 13 '15 at 18:54
  • Pls could you show an example. I've been on this for so long. I felt there's no need for such mappers as Gson and all since the response is already in json format. – Joseph Nov 13 '15 at 19:01
  • `JSON.parse(jsonMsg)` (and `JSON.stringify(value/object)`) can be used on the client-side, if you need not map the JSON response to a Java collection on the server side. – Tiny Nov 13 '15 at 19:11
  • Thanks again but i suppose you are better at JSF and you know i would need to populate the bean with the response to display. I want to map it to some collection on the server side. Am updating my code so you can see what am doing – Joseph Nov 13 '15 at 19:13
  • See [here](http://stackoverflow.com/a/10982930/1391249) especially the caution at the end of the answer. You can instead easily map the JSON response to a Java collection using an object mapper and use that collection in a normal Java way. (Using Gson, for example is basically the matter of doing `new Gson(). fromJson(JsonElement json, Class clazz)`). – Tiny Nov 13 '15 at 19:41
  • @BalusC. Yes i was able to display the raw json response on the browser but i want to be able to parse and response and display it in an HTML table – Joseph Nov 14 '15 at 12:43
  • @Tiny, I tried your solution of using new Gson().fromJson(JsonElement json, Class clazz). It gave errors saying it expected a Json object but an array is given. Recall the response is an array not an object – Joseph Nov 14 '15 at 13:15
  • So you basically want to convert JSON to Java? That part has nothing to do with JSF nor with REST services. I fixed the wrong question so you get the right attention. – BalusC Nov 14 '15 at 15:45
  • @BalusC Thanks for that – Joseph Nov 14 '15 at 17:07

0 Answers0