Thursday 4 April 2013

JSON to Object Mapping using Jackson

Leave a Comment

JSON:

{
"recentVideosList":[{"tags":null,"followerId":null,"videoId":"hLta6iJGtH8=","videoViewCount":"0","title":"twitter2","videoURL":"https://s3.amazonaws.com/youbusk/63/1364047039_011760.mov","userFollowingThisVideo":"No","videosByThisUser":"5","videoTimer":"6","videoUploadDateTime":"2013-03-25 07:41:49.0","isAlreadyFlagged":"N","uploadedByCountryId":"96","uploadedByUserId":"xHj2V0LLsfE=","thumbnailURL":"https://s3.amazonaws.com/youbusk/63/1364047039_011760.png","aboutUploadedBy":"ddddddddretreyreyrtyrt","tips":"0","uploadedBy":"naveen140990","abuseCount":"0","videoShareCount":"0","isAlreadyLiked":"NO"}],

"challengeOfTheDayList":[{"notificationId":"1","notificationDescription":"This is the first notification of videos."}],

"videoOfTheDayList":[{"tags":null,"followerId":null,"videoId":"jFB425S0Rv0=","videoViewCount":"9","title":"shr","videoURL":"https://s3.amazonaws.com/youbusk/2/1360314335_608470.mov","userFollowingThisVideo":"No","videosByThisUser":"6","videoTimer":"16","videoUploadDateTime":"2013-02-08 14:32:00.0","isAlreadyFlagged":"Y","uploadedByCountryId":"241","uploadedByUserId":"jFB425S0Rv0=","thumbnailURL":"https://s3.amazonaws.com/youbusk/2/1360314335_608470.png","aboutUploadedBy":"","tips":"14","uploadedBy":"gaurav11","abuseCount":"3","videoShareCount":"0","isAlreadyLiked":"YES"}],

"totalDataRequested":"2",
"totalDataSent":"2",
"responseCode":"4060",
"responseDescription":"Home service sent successfully."
}

JsonToObject.java

package com;

import java.io.IOException;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;


public class JsonToObject{

 public CustomBean jsonConvertor(String jsonString)
 {
  JsonFactory factory = new JsonFactory();
  ObjectMapper mapper = new ObjectMapper(factory);
  try 
  {
   CustomBean customBean = mapper.readValue(jsonString, CustomBean.class);
   
   return customBean;
  } catch (JsonParseException e) {
   e.printStackTrace();
  } catch (JsonMappingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
   return new CustomBean();
 }
}

CustomBean

package com;

import java.util.List;

public class CustomBean {

 private List recentVideosList;
 private List videoOfTheDayList;
 private List challengeOfTheDayList;
 private String totalDataRequested;
 private String totalDataSent;
 private String responseCode;
 private String responseDescription;
 
 public List getRecentVideosList() {
  return recentVideosList;
 }
 public void setRecentVideosList(List recentVideosList) {
  this.recentVideosList = recentVideosList;
 }
 public List getVideoOfTheDayList() {
  return videoOfTheDayList;
 }
 public void setVideoOfTheDayList(List videoOfTheDayList) {
  this.videoOfTheDayList = videoOfTheDayList;
 }
 public List getChallengeOfTheDayList() {
  return challengeOfTheDayList;
 }
 public void setChallengeOfTheDayList(
   List challengeOfTheDayList) {
  this.challengeOfTheDayList = challengeOfTheDayList;
 }
 public String getTotalDataRequested() {
  return totalDataRequested;
 }
 public void setTotalDataRequested(String totalDataRequested) {
  this.totalDataRequested = totalDataRequested;
 }
 public String getTotalDataSent() {
  return totalDataSent;
 }
 public void setTotalDataSent(String totalDataSent) {
  this.totalDataSent = totalDataSent;
 }
 public String getResponseCode() {
  return responseCode;
 }
 public void setResponseCode(String responseCode) {
  this.responseCode = responseCode;
 }
 public String getResponseDescription() {
  return responseDescription;
 }
 public void setResponseDescription(String responseDescription) {
  this.responseDescription = responseDescription;
 }
 
}


0 comments:

Post a Comment