Not so typical situation.
I have next JSON string from server:
{"SomeElements":[{"Id":"Title","Info":"{\"Text\": \"Sometext\"}"}]}
Here is graphic representation of it:
The problem is that string Info
contains another JSON
string.
Here is my POJO:
public class Test {
private ArrayList<SomeElement> SomeElements;
public class SomeElement {
private String Id;
private String Info;
}
}
Question - is there some way to parse Info
string not as String, but as HashMap (for example) in my POJO?
If I try to declare it as HashMap<String, String>
, I have an error "Expected OBJECT, but was STRING"
.
What is the best approach to handle this issue? Custom deserializer is the only way?
!! This question is NOT a duplicate !!
I cannot change JSON response from the server.
Please read carefully - I'm asking, is it possible to parse Info
string not as string, but as another JSON.
SOLUTION I ended up with next.
I declared private LinkedTreeMap<String, String> infoMap;
and Firstly deserialize Info
as String. Then:
public LinkedTreeMap<String, String> getInfoMap() {
if (infoMap == null) {
infoMap = new Gson().fromJson(info, new TypeToken<Map<String, String>>(){}.getType());
}
return infoMap;
}
I suppose this is much easier than writing custom deserializer, but maybe in more complex cases custom deserializer would be an only option.
So in general case answer of @arjabbar would work better.