JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Jackson API client - how to read json from URL?


This page shows how to read Json data from JSON API URLs using jackson APIs.

Note: Refer How to convert Java object to JSON string? page for dependent libraries.

Here is the input json file:

Json Input from URL: http://jsonplaceholder.typicode.com/posts/1
{
  "userId": 1,
  "id": 7,
  "title": "magnam facilis autem",
  "body": "dolore placeat quibusdam ea quo vitae"
}

package com.java2novice.json;

import java.io.IOException;
import java.net.URL;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.java2novice.models.Post;

public class ReadJsonEx {

	public static void main(String a[]){
		
		ObjectMapper mapper = new ObjectMapper();
		try {
			Post usrPost = mapper.readValue(new URL("http://jsonplaceholder.typicode.com/posts/7"), Post.class);
			System.out.println(usrPost);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

package com.java2novice.models;

public class Post {

	private int userId;
	private int id;
	private String title;
	private String body;
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String toString(){
		return this.userId+" | "+this.id +" | "+this.title+" | "+this.body; 
	}
}

Output:
1 | 7 | magnam facilis autem | dolore placeat quibusdam ea quo vitae
<< Previous Program 

Jackson JSON examples

  1. How to convert Java object to JSON string?
  2. How to convert JSON string to Java object?
  3. How to convert JSON string to Map using Jackson API?
  4. How to convert Map to JSON string using Jackson API?
  5. Enable JSON pretty print using Jackson API
  6. How to rename JSON properties using Jackson annotations?
  7. How to ignore JSON property using Jackson annotations?
  8. How to order JSON elements using Jackson annotations?
  9. How to ignore json empty or null values using Jackson API in java?
  10. How to handle date in Json using Jackson api in java?
  11. How to read specific json node in Jackson api (tree model)?
  12. Jackson API client - how to read json from URL?
Knowledge Centre
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.