Java JSON-Convert Java Object To/From JSON (Jackson)

Source: Internet
Author: User

Java JSON-Convert Java Object To/From JSON (Jackson)

JSON (JavaScript Object Notation) is a lightweight data exchange language based on text and easy to read. Although JSON is a subset of Javascript, JSON is a language-independent text format and is similar to the C language family. Wikipedia-http://zh.wikipedia.org/wiki/JSON


Jackson is a High-performance JSON processor Java library -- Jackson is a High-performance JSON-parsed Java lib


Core code:

//1. Convert Java object to JSON formatObjectMapper mapper = new ObjectMapper();mapper.writeValue(new File("c:\\user.json"), user);

//2. Convert JSON to Java objectObjectMapper mapper = new ObjectMapper();User user = mapper.readValue(new File("c:\\user.json"), User.class);


The following is an example:

I. POJOUser. java

package com.jiangge.jkstest;import java.util.ArrayList;import java.util.List;public class User {private int age = 23;private String name = "jiangge";private String qqNum = "499065469";private List
 
   message = new ArrayList
  
   () {{add("msg 1");add("msg 2");add("msg 3");}};public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List
   
     getMessage() {return message;}public void setMessage(List
    
      message) {this.message = message;}public String getqqNum() {return qqNum;}public void setqqNum(String qq) {this.qqNum = qq;}@Overridepublic String toString() {return "User [age=" + age + ", name=" + name + ", message=" + message+ "]";}}
    
   
  
 



Ii. Java Object to JSON
Convert an "user" object into JSON formatted string, and save it into a file "user. json".

package com.jiangge.jkstest;import java.io.File;import java.io.IOException;import com.fasterxml.jackson.core.JsonGenerationException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;//To make this work with the latest (1.9.3) Jackson, //there must be getters and setters for the members in the User class.public class JacksonExampleToJson {public static void main(String[] args) { User user = new User();ObjectMapper objMapper = new ObjectMapper();try {// convert user object to json string, and save to a fileobjMapper.writeValue(new File("c:\\user.json"), user);// display to consoleSystem.out.println(objMapper.writeValueAsString(user));} catch (JsonGenerationException e) {e.printStackTrace();} catch (JsonMappingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace(); }   }}
Console output:

{"Age": 23, "name": "jiangge", "qqNum": "499065469", "message": ["msg 1", "msg 2 ", "msg 3"]}




Iii. JSON to Java Object
Read JSON string from file "user. json", and convert it back to Java object.

package com.jiangge.jkstest;import java.io.File;import java.io.IOException;import com.fasterxml.jackson.core.JsonGenerationException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;public class JacksonExampleRead {public static void main(String[] args) { ObjectMapper objMapper = new ObjectMapper();try {// read from file, convert it to user classUser user = objMapper.readValue(new File("c:\\user.json"), User.class);// display to consoleSystem.out.println(user); } catch (JsonGenerationException e) {e.printStackTrace();} catch (JsonMappingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}   } }
Console output:

User [age = 23, name = jiangge, message = [msg 1, msg 2, msg 3]



References:

Http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

For more information, see:

Http://www.mkyong.com/tutorials/java-json-tutorials/





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.