Il est très courant de convertir un objet en chaîne au format Json dans les projets Java. Il existe de nombreuses boîtes à outils qui peuvent répondre à cette exigence, telles que Gson, JSON-lib , Jackson et. bientôt. Cet article présente principalement l'utilisation de Jackson. En plus de convertir des objets Java et des chaînes Json, Jackson peut également convertir des objets Java au format XML. Il est relativement simple à utiliser et est considéré comme relativement efficace.
Pour le package jar de Jackson, nous pouvons le télécharger à partir de la bibliothèque de ressources maven : //m.sbmmt.com/
Le package jar requis est le suivant, il suffit de le rechercher et de le télécharger par son nom.
Ensuite, pour écrire des cas de test, nous avons besoin d'une classe Java :
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 Convertir l'objet Java en chaîne 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(); } }
{"name":"Think in Java","price":100}
/* * Json转Java对象 */ @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(); } }
name:Think in Java; price:100
/* * Java对象转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(); } }
<Book xmlns=""><name>Think in Java</name><price>100</price></Book>
/* * xml转Java对象 */ @Test public void testGenObjByXml() { XmlMapper xmlMapper = new XmlMapper(); String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>"; try { Book book = xmlMapper.readValue(xmlStr, Book.class); System.out.println(book); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
name:Think in Java; price:100
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; /* * Java对象转 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(); } } /* * Json转Java对象 */ @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(); } } /* * Java对象转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(); } } /* * xml转Java对象 */ @Test public void testGenObjByXml() { XmlMapper xmlMapper = new XmlMapper(); String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>"; try { Book book = xmlMapper.readValue(xmlStr, Book.class); System.out.println(book); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Pour le package jar de Jackson, nous pouvons le télécharger à partir de la bibliothèque de ressources maven : //m.sbmmt.com/
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; } }
<🎜. >
Exécutez la méthode de test, sortie de la console :@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(); } }
{"name":"Think in Java","price":100}
/* * Json转Java对象 */ @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(); } }
name:Think in Java; price:100
/* * Java对象转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(); } }
<Book xmlns=""><name>Think in Java</name><price>100</price></Book>
/* * xml转Java对象 */ @Test public void testGenObjByXml() { XmlMapper xmlMapper = new XmlMapper(); String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>"; try { Book book = xmlMapper.readValue(xmlStr, Book.class); System.out.println(book); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
name:Think in Java; price:100
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; /* * Java对象转 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(); } } /* * Json转Java对象 */ @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(); } } /* * Java对象转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(); } } /* * xml转Java对象 */ @Test public void testGenObjByXml() { XmlMapper xmlMapper = new XmlMapper(); String xmlStr = "<Book><name>Think in Java</name><price>100</price></Book>"; try { Book book = xmlMapper.readValue(xmlStr, Book.class); System.out.println(book); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }