参考这篇文章 http://blog.csdn.net/u011425751/article/details/51219242
- - - ----------------------------------------------------------------------------------------------------------------------------
以下是Jackson使用
实体类注意注解:
@JsonProperty("nickname")
private String nickname;
public class JsonUtil { private static ObjectMapper mapper; public static void init() { mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES,false); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERscoreS); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")); } /** * 根据json格式转换对象 * @param content * @param valueType * @param <T> * @return * @throws IOException */ public static <T> T readValue(String content,Class<T> valueType) throws IOException { if(mapper == null){ init(); } return mapper.readValue(content,valueType); } /** * 将对象转换成json格式 * @param value * @return * @throws JsonProcessingException */ public static String writeValueAsString(Object value) throws JsonProcessingException { if(mapper == null){ init(); } return mapper.writeValueAsString(value); } }