改造2,杰克逊不能反编译内部jsonString&无法删除转义字符

时间:2017-12-13 12:07:59

标签: android json jackson retrofit2 jackson2

服务器响应数据:

{
    "data": "{\"domain\":{\"Logical\":\"small_logical.png\",\"Physical\":\"small_physical.png\"}"
}
  

onFailure:无法从字符串值中实例化[simple type,class com.mobile.app.model.Responses.Data]类型的值(' {" domain":{&#34 ;逻辑":

问题:

内部json对象以双引号开头,因此Jackson无法反序列化 - " {\" domain \":

我尝试使用jackson反序列化,但没有任何效果

public class RestClient {


    private static final String TAG = "RestClient";

    private static RestApi REST_CLIENT;

    public static RestApi get() {
        return REST_CLIENT;
    }

    public static String RestApiUrl() {
            return ROOT;
    }

    public static void setupRestClient() {


        ObjectMapper objectMapper = new ObjectMapper();

       SimpleModule module = new SimpleModule();
       module.addDeserializer(String.class, new ItemDeserializer());
       objectMapper.registerModule(module);


        Retrofit restAdapter = new Retrofit.Builder()
                .baseUrl(RestApiUrl())
                .client(client)
                .client(getUnsafeOkHttpClient())
                .addConverterFactory(JacksonConverterFactory.create(objectMapper)).build();
        REST_CLIENT = restAdapter.create(RestApi.class);
    }

}

public class ItemDeserializer  extends StdDeserializer<String> {

    private static final String TAG = "ItemDeserializer";

    public ItemDeserializer() {
        this(null);
    }

    public ItemDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();

        String jsonValue = jp.getValueAsString();
        JSONObject jsonObject = null;
        try {
             jsonObject = new JSONObject(jsonValue);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "deserializecustom: " + objectMapper.writeValueAsString(jsonObject));

        return objectMapper.writeValueAsString(jsonObject);
    }
}

0 个答案:

没有答案