2

Server Response data:

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

onFailure: Can not instantiate value of type [simple type, class com.mobile.app.model.Responses.Data] from String value ('{"domain":{"Logical":

Problem:

Inner json object starts with double quotes, so Jackson can't able to deserialize - "{\"domain\":

I tried with jackson deserialization, but nothing works with it

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);
    }
}
Chokkar G
  • 56
  • 6
  • Possible duplicate of [Jackson - How to process (deserialize) nested JSON?](https://stackoverflow.com/questions/11747370/jackson-how-to-process-deserialize-nested-json) – Zoe Dec 13 '17 at 12:13
  • I tried but not working. ('{"domain - Single quote added when deserializing using jackson. – Chokkar G Dec 13 '17 at 15:00

0 Answers0