欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json 转泛型的集合类

程序员文章站 2022-06-21 21:12:09
...

 

package com.boce.test;

 

import java.io.Serializable;

 

public class Man implements Serializable{

 

private String orderId;

 

private String result;

 

private String msg;

//无参数的构造函数是必须的

public Man(){

 

}

 

public Man(String orderId, String result, String msg) {

super();

this.orderId = orderId;

this.result = result;

this.msg = msg;

}

 

public String getOrderId() {

return orderId;

}

 

public void setOrderId(String orderId) {

this.orderId = orderId;

}

 

public String getResult() {

return result;

}

 

public void setResult(String result) {

this.result = result;

}

 

public String getMsg() {

return msg;

}

 

public void setMsg(String msg) {

this.msg = msg;

}

 

 

}

 

 

package com.boce.test;

 

import java.io.IOException;

import java.util.List;

 

import com.fasterxml.jackson.core.JsonParser.Feature;

import com.fasterxml.jackson.databind.DeserializationFeature;

import com.fasterxml.jackson.databind.JavaType;

import com.fasterxml.jackson.databind.ObjectMapper;

 

public class JsonToCollection {

/**

* 字符串转化为集合类型的泛型 如ArrayList<*****> 

* @param data 

* @param collectionClass  ArrayList.class

*  @param  clazz   ***.class

* @return

*/

public Object jsonToListObject(final String data,Class<?> collectionClass, Class<?> ...clazz){

Object obj = null;

try {

// string to Object ObjectMapper类用序列化与反序列化映射器

ObjectMapper mapper = new ObjectMapper();

//获取泛型的Collection Type

JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass, clazz); 

// 当反序列化json时,未知属性会引起的反序列化被打断,这里我们禁用未知属性打断反序列化功能,

// 因为,例如json里有10个属性,而我们的bean中只定义了2个属性,其它8个属性将被忽略

mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

// 从json映射到java对象,得到country对象后就可以遍历查找,下面遍历部分内容,能说明问题就可以了

 

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;

 

obj = mapper.readValue(data, javaType);

} catch (IOException io) {

io.printStackTrace();

}

 

return obj;

}

 

/**

* 字符串转化为集合类型的泛型 如ArrayList<*****> 

* @param data 

* @param collectionClass  HashMap.class

*  @param  zlass   ***.class

*  @param  clazz   ***.class

* @return

*/

public Object jsonToHashMapObject(final String data,Class<?> collectionClass,Class<?> zlass, Class<?> clazz){

Object obj = null;

try {

// string to Object ObjectMapper类用序列化与反序列化映射器

ObjectMapper mapper = new ObjectMapper();

//获取泛型的Collection Type

JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass,zlass,clazz); 

// 当反序列化json时,未知属性会引起的反序列化被打断,这里我们禁用未知属性打断反序列化功能,

// 因为,例如json里有10个属性,而我们的bean中只定义了2个属性,其它8个属性将被忽略

mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

// 从json映射到java对象,得到country对象后就可以遍历查找,下面遍历部分内容,能说明问题就可以了

 

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;

 

obj = mapper.readValue(data, javaType);

} catch (IOException io) {

io.printStackTrace();

}

 

return obj;

}

 

public static void main(String[] args) {

 

JsonToCollection json = new JsonToCollection();

String data ="[{\"msg\":\"成功\",\"orderId\":\"17030700918003\",\"result\":\"true\"}]";

 

List<Man> list = (List<Man>)json.jsonToListObject(data, List.class, Man.class);

for(Man it:list){

System.out.println(it.getOrderId() +"--"+it.getResult()+"---"+it.getMsg());

}

 

}

 

}