jsonUtils&&Json、Xml转换工具Jackson使用

时间:2023-12-09 20:38:43

1.jsonUtils

package com.icil.utils;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/** 需要的依赖(jar) <!-- Jackson Json处理工具包 用于处理bean 到 json json dao bean -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency> */ /**
* 自定义响应结构(pojo to json to pojo)
*/
public class JsonUtils { // 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper(); /**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
} /**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
} return null;
} }
package com.icil.esolution.utils;
import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; /**
* * @ClassName: JsonUtils * @Description:json to bean object to json * @author Sea * @date 24 Aug 2018 2:12:27 PM
*
*/
public class JsonUtils { // 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper(); /**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
} /**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
} return null;
} /**
* 可以排除多余的字段,如json中包含字段"aa":"aa" 但是pojo中没有.
* @param json
* @param beanType
* @return
*/
public static <T>List<T> JsonTOList(String json, Class<T> beanType){
Gson gson = new Gson();
List<T> jsonToList =gson.fromJson(json, new TypeToken<List<T>>() {}.getType());
return jsonToList;
} }

2.Java对象、Json、Xml转换工具Jackson使用 参考https://blog.csdn.net/Rongbo_J/article/details/47986121

依赖:

<!-- Jackson Json处理工具包 用于处理bean 到 json json dao bean -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<!-- 转json 和json转bean用到-->
     <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<!-- 转xml 和xml转bean用到-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.</version>
</dependency>

2.1.將Java对象转换为Json字符串:

@Test
public void testGenJson()
{
ObjectMapper objMapper = new ObjectMapper();
Book book = new Book("Think in Java",);
try {
jsonGen = objMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
jsonGen.writeObject(book);
} catch (IOException e) {
e.printStackTrace();
}
}

2.2將Json字符串转为Java对象:

     /*
* Java对象转xml
*/
@Test
public void testGenXml()
{
XmlMapper xmlMapper = new XmlMapper(); Book book = new Book("Think in Java",);
try {
String xmlStr = xmlMapper.writeValueAsString(book);
System.out.println(xmlStr);
} catch (JsonProcessingException 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();
}
}

2.3將Java对象转为Xml格式

 /*
* Java对象转xml
*/
@Test
public void testGenXml()
{
XmlMapper xmlMapper = new XmlMapper(); Book book = new Book("Think in Java",);
try {
String xmlStr = xmlMapper.writeValueAsString(book);
System.out.println(xmlStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}

4.將xml格式字符串转为Java对象

  /*
* 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();
} }