-1

I am a JAVA newbie and I have never dealt with JSON data before. I have to create a news desktop application and for that I sent a GET request to a web address and I get JSON output like shown below.

 ****OUTPUT in JSON****

    {
    "status": "ok",
    "source": "the-hindu",
    "sortBy": "top",
    -"articles": [
    -{
    "author": "AP",
    "title": "Liu Xiaobo, Chinese dissident who won Nobel prize, dies",
    "description": "He succumbed to cancer in a hospital while remaining under 
    police custody.",
    "url": "http://www.thehindu.com/news/international/chinese-nobel-laureate-
    liu-xiaobo-dies/article19271778.ece",

    "urlToImage":"http://www.thehindu.com/news/international/Xiaobo",
    "publishedAt": null
    },

    -{
    "author": "Pavan Dahat",
    "title": "BJP leader beaten for carrying ‘beef’",
    "description": "A video of the incident that went viral on social media, 
    shows the attackers accusing the man of “ killing a cow.”",
    "url": "http://www.thehindu.com/news/national/other-states/man-attacked-
     in-
    nagpur-for-allegedly-carrying-beef/article19269863.ece",
    "urlToImage": "http://www.thehindu.com/news/national/other-
    states/article19269862.ece/ALTERNATES/LANDSCAPE_615/beef1",
    "publishedAt": null
    },

    ]
   }

Now I have to parse this JSON data, and arrange the headlines with the images in their proper positions.How can I do that ?

1 Answers1

0

You can do this easily with jackson2:

first add jackson2 as a dependency in your app:

An example saving it into an array list:

//Declare an array list of the object that you want to use    
private List<YourOwnObject> cart = new ArrayList<>();

//Create an instance of ObjectMapper
ObjectMapper mapper = new ObjectMapper();

//Third pass your json string to the object mapper
this.cart = mapper.readValue(yourJsonResponseString, mapper.getTypeFactory().constructCollectionType(List.class, YourOwnObject.class));

Hope this helps! regards,

Ivan Lynch
  • 563
  • 5
  • 23