Home > front end >  How can I Parse an Object which is an array of JSONS coming from an URL?
How can I Parse an Object which is an array of JSONS coming from an URL?

Time:10-08

I am calling an URL and getting a response like shown below. I am not able to parse it. Can someone please tell me How can I parse the objects in the array?

Object[] response = restTemplate.exchange(url, HttpMethod.GET, item, Object[].class);

JSON:

[
   {
      "name":"abc",
      "description":"Data",
      "createdBy":"abc",
      "id":"30D8000506",
      "models":[
         {
            "description":"Data",
            "id":"dcfdf",
            "Planning":false
         }
      ],
      "changedBy":"abc",
      "openURL":"ABC"
   },
   {
      "name":"louis",
      "description":"",
      "createdBy":"abc",
      "id":"583E3817216",
      "changed":"abc",
      "URL":"ABC"
   },
   {
      "name":"Board",
      "description":"",
      "createdBy":"abc",
      "id":"583E39BB21",
      "changedBy":"abc",
      "openURL":"ABC"
   }
]

CodePudding user response:

There are several ways.

  1. Build the classes for your response objects & use Jackson to map this response into an array of POJOs. This would take more time but it would make working with the response way easier.

  2. Use a JSON library and parse the response using the library. Then get only the values and attributes that you need...

Small example:

// Get your Json Array 

JSONArray itemArray = new JSONArray(yourJsonString); // Here is your response...

// Get your first Element from your array 

JSONObject firstItem = itemArray.getJSONObject(0); // That would be  "name":"abc", "description":"Data" etc...

// Access any value on your jsonObject

String itemName = firstItem.getString("name"); // "name", "description" , "data" and so on
  • Related