-
How to convert Model Object into JSON string and JSON string into Model Object in Java?
over 9 years ago
over 9 years ago
Sometimes we need to get JSON string direct from Model object and to read JSON values from Model object. We can do this functionality very easily by using Jackson Mapper.
Follow to steps in order to do conversion from Model Object to JSON string and from JSON string to Model object.
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.11</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.9.11</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-jaxrs</artifactId>
- <version>1.9.11</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-xc</artifactId>
- <version>1.9.11</version>
- </dependency>
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.11</version> </dependency>
SubjectListModel.java
- @JsonIgnoreProperties(ignoreUnknown = true)
- @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
- public class SubjectListModel {
- private List<String> subjectList;
- public List<String> getSubjectList() {
- return subjectList;
- }
- public void setSubjectList(List<String> subjectList) {
- this.subjectList = subjectList;
- }
- }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT) public class SubjectListModel { private List<String> subjectList; public List<String> getSubjectList() { return subjectList; } public void setSubjectList(List<String> subjectList) { this.subjectList = subjectList; } }
- /**
- * Get JSON string from an object
- * @param object
- * @return
- */
- public static String getJsonFromObject(Object object)
- {
- ObjectMapper mapper = new ObjectMapper();
- String jsonString = null;
- try
- {
- if (object != null)
- jsonString = mapper.defaultPrettyPrintingWriter().writeValueAsString(object);
- }
- catch (JsonGenerationException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (JsonMappingException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // jsonString = URLEncoder.encode(jsonString, "UTF-8");
- return jsonString;
- }
/** * Get JSON string from an object * @param object * @return */ public static String getJsonFromObject(Object object) { ObjectMapper mapper = new ObjectMapper(); String jsonString = null; try { if (object != null) jsonString = mapper.defaultPrettyPrintingWriter().writeValueAsString(object); } catch (JsonGenerationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // jsonString = URLEncoder.encode(jsonString, "UTF-8"); return jsonString; }
Now we'll use the above function as below:
- SubjectListModel subjectListModel = new SubjectListModel();
- List<String> subjectList = new ArrayList<String>();
- subjectList.add("Graffiti");
- subjectList.add("Pothole");
- subjectList.add("Street Light Defect");
- subjectListModel.setSubjectList(subjectList);
- //Call the above function
- String jsonString = getJsonFromObject(subjectListModel);
- System.out.println(jsonString);
SubjectListModel subjectListModel = new SubjectListModel(); List<String> subjectList = new ArrayList<String>(); subjectList.add("Graffiti"); subjectList.add("Pothole"); subjectList.add("Street Light Defect"); subjectListModel.setSubjectList(subjectList); //Call the above function String jsonString = getJsonFromObject(subjectListModel); System.out.println(jsonString);
Output:
- /**
- * Get Model Object from JSON string
- * @param JsonString
- * @return
- */
- public static Object getObjectFromJson(String JsonString)
- {
- UpdatedTickitHistoryModel updatedTickitHistoryModel = null;
- try
- {
- if (JsonString != null && !JsonString.isEmpty())
- updatedTickitHistoryModel = new ObjectMapper().readValue(JsonString, UpdatedTickitHistoryModel.class);
- }
- catch (JsonParseException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (JsonMappingException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return updatedTickitHistoryModel;
- }
/** * Get Model Object from JSON string * @param JsonString * @return */ public static Object getObjectFromJson(String JsonString) { UpdatedTickitHistoryModel updatedTickitHistoryModel = null; try { if (JsonString != null && !JsonString.isEmpty()) updatedTickitHistoryModel = new ObjectMapper().readValue(JsonString, UpdatedTickitHistoryModel.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return updatedTickitHistoryModel; }
now we'll get SubjectListModel object from jsonstring here by usinf above function:
- String jsonString = {"subjectList" : [ "Graffiti", "Pothole", "Street Light Defect" ]};
- SubjectListModel subjectListModel = (SubjectListModel) getObjectFromJsonForSubjectList(jsonString);
String jsonString = {"subjectList" : [ "Graffiti", "Pothole", "Street Light Defect" ]}; SubjectListModel subjectListModel = (SubjectListModel) getObjectFromJsonForSubjectList(jsonString);
Hope this will help you :)
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)