Question: I have a Json string which has a string message field.
String:

```
{  "Type" : "Text",
   "Subject" : "data received",
   "Message" :"{\\"language\\":\\"US\\",\\"data\\":\\"signature\\"}"
}

```

I want to convert it into the following structure:
Notification.java

```
public class Notification {
  String type;
  String subject;
  Message message;
}

```

Message.java

```
public class Message {
  String language;
  String data;
}

```

Is there a way in which I can directly convert the string to a Java object of the above structure? I want to avoid deserializing twice.

Answer: 
You can create a custom `Deserializer` to deserialize the `Message` text into `Message` object and annotate the `Message` class with `@JsonDeserialize`:

```
@JsonDeserialize(using = MessageDeserializer.class)
public class Message {
    String language;
    String data;
}

public class MessageDeserializer extends JsonDeserializer<Message> {

    public MessageDeserializer() {
        super();
    }

    @Override
    public Message deserialize(
            final JsonParser jsonParser, final DeserializationContext deserializationContext) throws
            IOException, JsonProcessingException {
        final String messageText = jsonParser.getText(); 
        // parse messageText into Message object
    }
}

```
You can convert json string into key-value pairs in Map.You will have to do twice as the `Message` value is again a json string.Use `org.json` for JSONObject

```
    Map<String, String> map = new HashMap<String, String>();
    JSONObject j = new JSONObject(str);
    Iterator<String> keys = j.keys();

    while( keys.hasNext() ){
        String key = (String)keys.next();
        String val = j.getString(key); 
        map.put(key, val);}

```

Then retrieve the values by iterating over the keys and pass the values into the class constructor

Then `map.get(key)` can be used to retrieve the values and will be passed into constructors of the classes.