jackson 学习

1.json 转为object对象
public class CouponMessageConverter implements MessageConverter {
    private static ObjectMapper objectMapper= new ObjectMapper();
    static {
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        objectMapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
        objectMapper.setDateFormat(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"));
    }
    @Override
    public Message toMessage(Object object, Session session)
            throws JMSException, MessageConversionException {

        throw new RuntimeException("not implemented");
    }

    @Override
    public Object fromMessage(Message message) throws JMSException, MessageConversionException {
        String text = ((TextMessage) message).getText();
        try {
          return  objectMapper.readValue(text, CouponMessage.class);
        }catch (Exception e){
            logger.error("CouponMessageConverter.fromMessage() 消息转化错误。"+e);
        }
        return null;
    }
    public static void main(String[] args)throws Exception{
        String s ="{\"id\":\"59602295\",\"pin\":\"Jingdong5950\",\"couponType\":\"0\",\"pici\":388,\"piciUsed\":0,\"createtime\":\"11/07/2012 11:33:32\",\"beginTime\":\"11/07/2012 11:33:33\",\"endtime\":\"11/05/2013 23:59:00\",\"parvalue\":20.0000,\"quota\":0.0000,\"state\":2,\"SystemType\":\"aaaaa\",\"MessageType\":\"aaaaa\",\"sourceid\":0,\"sourcenum\":0,\"rftype\":103,\"auditId\":69879681,\"returnTime\":\"11/07/2012 11:33:32\",\"useTime\":\"01/01/0001 00:00:00\",\"OrderDate\":\"01/01/0001 00:00:00\",\"OrderId\":0}";
        CouponMessage couponMessage = objectMapper.readValue(s, CouponMessage.class);
        System.out.println(couponMessage.getId());
    }
    private final static Logger logger = Logger.getLogger(CouponMessageReceiveHandler.class);
}

猜你喜欢

转载自kevin1.iteye.com/blog/1724681