Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert Model Object into JSON string and JSON string into Model Object in Java?

    • 0
    • 3
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 584
    Comment on it

    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.

    1. You need to declare the below dependency for Jackson JSON Mapper in your pom.xml file:
    <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> 
    
    1. Now define the model class as below:

    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;
        }
    
    }
    
    1. Write the below function to JSON string from Model Object
    /**
     * 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);
    

    Output:

    {"subjectList" : [ "Graffiti", "Pothole", "Street Light Defect" ]}
    
    1. Write the below code to get SubjectListModel model object from JSON string:
       /**
         * 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);
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: