In Java projects, it is very common to convert an object into a string in Json format. many toolkits can meet this requirement, such as Gson, JSON-lib, and Jackson. This article mainly introduces the use of Jackson. in addition to implementing the conversion between Java objects and Json strings, Jackson can also convert Java objects into Xml format, which is simple to use and is said to be more efficient.
In Java projects, it is very common to convert an object into a string in Json format. many toolkits can meet this requirement, such as Gson, JSON-lib, and Jackson. This article mainly introduces the use of Jackson. in addition to implementing the conversion between Java objects and Json strings, Jackson can also convert Java objects into Xml format, which is simple to use and is said to be more efficient.
For the Jackson jar package, we can download it from the maven Repository :#
The jar package is as follows. you can search and download it by name.
Next, compile the test case. we need a java class:
package com.csii.jackson.object;public class Book{ private String name; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Book() { } public Book(String name,int price) { this.name = name; this.price = price; } @Override public String toString() { return "name:" + name +"; price:" + price; }}
1. convert a Java object to a Json string:
@Test public void testGenJson() { ObjectMapper objMapper = new ObjectMapper(); Book book = new Book("Think in Java",100); try { jsonGen = objMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8); jsonGen.writeObject(book); } catch (IOException e) { e.printStackTrace(); } }
Run the test method. the console outputs:
{"name":"Think in Java","price":100}
2. convert the Json string into a Java object:
/** Convert Json to Java object */@ Test public void testGenObjByJson () {ObjectMapper objMapper = new ObjectMapper (); String json = "{\" name \": \ "Think in Java \", \ "price \": 100} "; try {Book book = objMapper. readValue (json, Book. class); System. out. println (book);} catch (IOException e) {e. printStackTrace ();}}
Because we have rewritten the toString method of the Book class, run the test method, and output the console:
name:Think in Java; price:100
3. convert the Java Object to Xml format:
/** Convert Java objects to xml */@ Test public void testGenXml () {XmlMapper xmlMapper = new XmlMapper (); Book book = new Book ("Think in Java", 100 ); try {String xmlStr = xmlMapper. writeValueAsString (book); System. out. println (xmlStr);} catch (JsonProcessingException e) {e. printStackTrace ();}}
Run the test method. the console outputs:
Think in Java
100
4. convert an xml string to a Java object:
/** Convert xml to Java object */@ Test public void testGenObjByXml () {XmlMapper xmlMapper = new XmlMapper (); String xmlStr ="
Think in Java
100
"; Try {Book book = xmlMapper. readValue (xmlStr, Book. class); System. out. println (book);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}
Output Content:
name:Think in Java; price:100
Complete test case code:
Package com. csii. jackson. test; import java. io. IOException; import org. junit. test; import com. csii. jackson. object. book; import com. fasterxml. jackson. core. jsonEncoding; import com. fasterxml. jackson. core. jsonGenerator; import com. fasterxml. jackson. core. jsonProcessingException; import com. fasterxml. jackson. databind. objectMapper; import com. fasterxml. jackson. dataformat. xml. xmlMapper; @ SuppressWarnings ("deprecation") public class JsonTest {private JsonGenerator jsonGen = null;/** convert the Java Object to Json */@ Test public void testGenJson () {ObjectMapper objMapper = new ObjectMapper (); Book book = new Book ("Think in Java", 100); try {jsonGen = objMapper. getJsonFactory (). createJsonGenerator (System. out, JsonEncoding. UTF8); jsonGen. writeObject (book);} catch (IOException e) {e. printStackTrace () ;}/ ** convert Json to Java object */@ Test public void testGenObjByJson () {ObjectMapper objMapper = new ObjectMapper (); string json = "{\" name \ ": \" Think in Java \ ", \" price \ ": 100}"; try {Book = objMapper. readValue (json, Book. class); System. out. println (book);} catch (IOException e) {e. printStackTrace () ;}/ ** convert Java objects to xml */@ Test public void testGenXml () {XmlMapper xmlMapper = new XmlMapper (); book book = new Book ("Think in Java", 100); try {String xmlStr = xmlMapper. writeValueAsString (book); System. out. println (xmlStr);} catch (JsonProcessingException e) {e. printStackTrace () ;}/ ** convert xml to Java object */@ Test public void testGenObjByXml () {XmlMapper xmlMapper = new XmlMapper (); String xmlStr ="
Think in Java
100
"; Try {Book book = xmlMapper. readValue (xmlStr, Book. class); System. out. println (book);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
In Java projects, it is very common to convert an object into a string in Json format. many toolkits can meet this requirement, such as Gson, JSON-lib, and Jackson. This article mainly introduces the use of Jackson. in addition to implementing the conversion between Java objects and Json strings, Jackson can also convert Java objects into Xml format, which is simple to use and is said to be more efficient.
For the Jackson jar package, we can download it from the maven Repository :#
The jar package is as follows. you can search and download it by name.
Next, compile the test case. we need a java class:
package com.csii.jackson.object;public class Book{ private String name; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Book() { } public Book(String name,int price) { this.name = name; this.price = price; } @Override public String toString() { return "name:" + name +"; price:" + price; }}
1. convert a Java object to a Json string:
@Test public void testGenJson() { ObjectMapper objMapper = new ObjectMapper(); Book book = new Book("Think in Java",100); try { jsonGen = objMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8); jsonGen.writeObject(book); } catch (IOException e) { e.printStackTrace(); } }
Run the test method. the console outputs:
{"name":"Think in Java","price":100}
2. convert the Json string into a Java object:
/** Convert Json to Java object */@ Test public void testGenObjByJson () {ObjectMapper objMapper = new ObjectMapper (); String json = "{\" name \": \ "Think in Java \", \ "price \": 100} "; try {Book book = objMapper. readValue (json, Book. class); System. out. println (book);} catch (IOException e) {e. printStackTrace ();}}
Because we have rewritten the toString method of the Book class, run the test method, and output the console:
name:Think in Java; price:100
3. convert the Java Object to Xml format:
/** Convert Java objects to xml */@ Test public void testGenXml () {XmlMapper xmlMapper = new XmlMapper (); Book book = new Book ("Think in Java", 100 ); try {String xmlStr = xmlMapper. writeValueAsString (book); System. out. println (xmlStr);} catch (JsonProcessingException e) {e. printStackTrace ();}}
Run the test method. the console outputs:
Think in Java
100
4. convert an xml string to a Java object:
/** Convert xml to Java object */@ Test public void testGenObjByXml () {XmlMapper xmlMapper = new XmlMapper (); String xmlStr ="
Think in Java
100
"; Try {Book book = xmlMapper. readValue (xmlStr, Book. class); System. out. println (book);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}
Output Content:
name:Think in Java; price:100
Complete test case code:
Package com. csii. jackson. test; import java. io. IOException; import org. junit. test; import com. csii. jackson. object. book; import com. fasterxml. jackson. core. jsonEncoding; import com. fasterxml. jackson. core. jsonGenerator; import com. fasterxml. jackson. core. jsonProcessingException; import com. fasterxml. jackson. databind. objectMapper; import com. fasterxml. jackson. dataformat. xml. xmlMapper; @ SuppressWarnings ("deprecation") public class JsonTest {private JsonGenerator jsonGen = null;/** convert the Java Object to Json */@ Test public void testGenJson () {ObjectMapper objMapper = new ObjectMapper (); Book book = new Book ("Think in Java", 100); try {jsonGen = objMapper. getJsonFactory (). createJsonGenerator (System. out, JsonEncoding. UTF8); jsonGen. writeObject (book);} catch (IOException e) {e. printStackTrace () ;}/ ** convert Json to Java object */@ Test public void testGenObjByJson () {ObjectMapper objMapper = new ObjectMapper (); string json = "{\" name \ ": \" Think in Java \ ", \" price \ ": 100}"; try {Book = objMapper. readValue (json, Book. class); System. out. println (book);} catch (IOException e) {e. printStackTrace () ;}/ ** convert Java objects to xml */@ Test public void testGenXml () {XmlMapper xmlMapper = new XmlMapper (); book book = new Book ("Think in Java", 100); try {String xmlStr = xmlMapper. writeValueAsString (book); System. out. println (xmlStr);} catch (JsonProcessingException e) {e. printStackTrace () ;}/ ** convert xml to Java object */@ Test public void testGenObjByXml () {XmlMapper xmlMapper = new XmlMapper (); String xmlStr ="
Think in Java
100
"; Try {Book book = xmlMapper. readValue (xmlStr, Book. class); System. out. println (book);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
The above content is used by Jackson, a Java object, Json, and Xml conversion tool. For more information, see PHP Chinese network (www.php1.cn )!