HTTP请求获得的数据(json或xml)进行类型转换(String 转 int )

//有一个参数的返回的是string类型的字符串,我想用int接收,提供给后面使用,需写好转换逻辑

static class StringDeserializer extends JsonDeserializer<Integer> { @Override public Integer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { return (jp.getText() == null || "".equals(jp.getText())) ? 0 : 1; } }

 

在该字段上加注解java

@JsonDeserialize(using = StringDeserializer.class)
private int errorCode;ide

 

 

若是是xml类型的数据(String转money)spa

 static class MoneyAdapter extends XmlAdapter<String, Money> {

        /**
         * {@inheritDoc}
         * 
         * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
         */
        @Override
        public Money unmarshal(String v) throws Exception {
            return new Money(v);
        }

        /**
         * {@inheritDoc}
         * 
         * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
         */
        @Override
        public String marshal(Money v) throws Exception {
            return v.toString();
        }

    }

在相应字段加注解,就好了 code

@XmlJavaTypeAdapter(MoneyAdapter.class)
@XmlElement(name = "remain_value")
private Money remainValue;xml

相关文章
相关标签/搜索