package com.lsm.app; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.MapperConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.PropertyNamingStrategy; import org.codehaus.jackson.map.introspect.AnnotatedMethod; import org.codehaus.jackson.type.JavaType; import org.codehaus.jackson.type.TypeReference; public class JsonUtil { @Deprecated//表示过时的方法 public String JSON_Object2String(Object ob) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(ob); } @Deprecated public List<?> JSON_String2List(String jsonString, Class<?> cls) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, cls); List ls = (List) mapper.readValue(jsonString, javaType); return ls; } public Collection JSON_String2Collection(String jsonString) throws JsonParseException, JsonMappingException, IOException { Collection readValues = new ObjectMapper().readValue(jsonString, new TypeReference<Collection>() { }); return readValues; } @Deprecated public Object JSON_String2Object(String jsonString, Class<?> cls) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jsonString, cls); } public Object JSON_byte2List(byte[] jsonData, Class<?> cls) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, cls); List ls = (List) mapper.readValue(jsonData, javaType); return ls; } public byte[] JSON_Object2Byte(Object ob) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsBytes(ob); } public static Object JSON_byte2Object(byte[] jsonData, Class<?> cls) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); //设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性(这只是其中一种方法,还可以在类上注解) // mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper.readValue(jsonData, cls); } public static byte[] JSON_Object2Byte2(Object ob) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy() { private static final long serialVersionUID = 1L; // 反序列化时调用 @Override public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) { return method.getName().substring(3); } // 序列化时调用 @Override public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) { return method.getName().substring(3); } }); return objectMapper.writeValueAsBytes(ob); } } //返回的实体类如下 import org.codehaus.jackson.annotate.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true)//忽略json中有的字段而实体类没有 public class RequestResultModel { private boolean success; private Object msg; private Object rd; // get,set方法忽略。 } 调用代码如下 public static void main(String[] args) { SpringApplication.run(App.class, args); try { String jsonStr="{\"success\":\"true\",\"msg\":\"操作成功\",\"error\":\"错误信息\"}"; byte[] respDate= jsonStr.getBytes(); RequestResultModel result; result = (RequestResultModel) JsonUtil.JSON_byte2Object(respDate, RequestResultModel.class); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } //打印结果 RequestResultModel [success=true, msg=操作成功, date=null, rd=null] |