Convert Java Object into JSON and JSON into Java Object | Jackson API
In this video I have shown how to parse JSON Object to a JAVA Object using JACKSON API.
- Project Structure,
-
In this project we use 2 jars,
1. com.fasterxml.jackson.core.jar
2. com.fasterxml.jackson.databind.jar
Add these jars to build path of Project -
For testing we have registered with website http://api.ipinfodb.com. After registering we will get the API Key.This website has a web service which will return JSON response of IP location information for the HTTP request we send.
Eg: HTTP request
http://api.ipinfodb.com/v3/ip-city/?key=9d64fcfdfacc213c7ddf4ef911dfe97b55e4696be3532bf8302846c09ebad06b&ip=37.210.57.113&format=json
Response -
Create a Bean class with all the parameters of the JSON object
Country_Bean.java
package com.chillyfacts.com; public class Country_Bean { private String statusCode; private String statusMessage; private String ipAddress; private String countryCode; private String countryName; private String regionName; private String cityName; private String zipCode; private String latitude; private String longitude; private String timeZone; public String getStatusCode() { return statusCode; } public void setStatusCode(String statusCode) { this.statusCode = statusCode; } public String getStatusMessage() { return statusMessage; } public void setStatusMessage(String statusMessage) { this.statusMessage = statusMessage; } public String getIpAddress() { return ipAddress; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public String getRegionName() { return regionName; } public void setRegionName(String regionName) { this.regionName = regionName; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getLatitude() { return latitude; } public void setLatitude(String latitude) { this.latitude = latitude; } public String getLongitude() { return longitude; } public void setLongitude(String longitude) { this.longitude = longitude; } public String getTimeZone() { return timeZone; } public void setTimeZone(String timeZone) { this.timeZone = timeZone; } }
- JSON_to_JAVA.java
package com.chillyfacts.com; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.codehaus.jackson.map.ObjectMapper; public class JSON_to_JAVA { public static void main(String[] args) { try { JSON_to_JAVA.convert_json_to_java(); } catch (Exception e) { System.out.println(e); } } public static void convert_json_to_java() throws Exception { String ip = "37.210.57.113"; String key = "9d64fcfdfacc213c7ddf4ef911dfe97b55e4696be546x532bf8302876c09ebad06b"; String url = "http://api.ipinfodb.com/v3/ip-city/?key=" + key + "&ip=" + ip + "&format=json"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in .readLine()) != null) { response.append(inputLine); } in .close(); System.out.println(response.toString()); ObjectMapper obj_ObjectMapper = new ObjectMapper(); Country_Bean obj_Country_Bean = new Country_Bean(); obj_Country_Bean = obj_ObjectMapper.readValue(response.toString(), Country_Bean.class); System.out.println("-------Afer converting to java object-------------------"); System.out.println("statusCode-" + obj_Country_Bean.getStatusCode()); System.out.println("statusMessage-" + obj_Country_Bean.getStatusMessage()); System.out.println("ipAddress-" + obj_Country_Bean.getIpAddress()); System.out.println("countryCode-" + obj_Country_Bean.getCountryCode()); System.out.println("countryName-" + obj_Country_Bean.getCountryName()); System.out.println("regionName-" + obj_Country_Bean.getRegionName()); System.out.println("cityName-" + obj_Country_Bean.getCityName()); System.out.println("zipCode-" + obj_Country_Bean.getZipCode()); System.out.println("latitude-" + obj_Country_Bean.getLatitude()); System.out.println("longitude-" + obj_Country_Bean.getLongitude()); System.out.println("timeZone-" + obj_Country_Bean.getTimeZone()); } }
- Console Output after converting the JSON response to JAVA object
{ "statusCode" : "OK", "statusMessage" : "", "ipAddress" : "37.210.57.113", "countryCode" : "QA", "countryName" : "Qatar", "regionName" : "Ad Dawhah", "cityName" : "Doha", "zipCode" : "-", "latitude" : "25.2793", "longitude" : "51.5224", "timeZone" : "+03:00"} -------Afer converting to java object------------------- statusCode-OK statusMessage- ipAddress-37.210.57.113 countryCode-QA countryName-Qatar regionName-Ad Dawhah cityName-Doha zipCode-- latitude-25.2793 longitude-51.5224 timeZone-+03:00
- JAVA_to_JSON.java
package com.chillyfacts.com; import java.io.StringWriter; import org.codehaus.jackson.map.ObjectMapper; public class JAVA_to_JSON { public static void main(String[] args) { try { Country_Bean obj_Country_Bean = new Country_Bean(); obj_Country_Bean.setStatusCode("OK"); obj_Country_Bean.setStatusMessage("jinu Testing JSON"); obj_Country_Bean.setCountryName("INDIA"); obj_Country_Bean.setIpAddress("2.2.2.2.2.2"); obj_Country_Bean.setCountryCode("IN"); obj_Country_Bean.setRegionName("Kerala"); obj_Country_Bean.setCityName("Kerala City"); obj_Country_Bean.setZipCode("9632874"); obj_Country_Bean.setLatitude("11.3656155"); obj_Country_Bean.setLongitude("52.3333"); obj_Country_Bean.setTimeZone("test time zone"); ObjectMapper objectMapper = new ObjectMapper(); StringWriter stringEmp = new StringWriter(); objectMapper.writeValue(stringEmp, obj_Country_Bean); System.out.println("Country JSON is\n" + stringEmp); } catch (Exception e) { System.out.println(e); } } }
- Console output after converting Java Object to JSON
Country JSON is {"statusCode":"OK","statusMessage":"jinu Testing JSON","ipAddress":"2.2.2.2.2.2","countryCode":"IN","countryName":"INDIA","regionName":"Kerala","cityName":"Kerala City","zipCode":"9632874","latitude":"11.3656155","longitude":"52.3333","timeZone":"test time zone"}
-
Download the project Here
JSON_JAVA.rar
0 Comments
Comments
Leave a Comment