0

I have a scenario where I need to deserialise a JSON array to a list of strings.

My class is:

public class Pojo {  
  private String message;  
  private ArrayList<String> details;  
}

JSON payload:

{
"message":"some value",
"details":[]    
}

If I try to deserialise the below JSON, it works:

{
"message":"working example",
"details": [ 
"First element of the array", 
"Second element"
]
}

In this case:

details = ["First element of the array", "Second element"]

If the array contains JSON objects as elements it will fail with MismatchedInputException:

{
  "message":"working example",
  "details":[ 
    {"First":"Value"}, 
    "Second element"
]}

For the second example, I'm expecting

details = ["{\"First\":\"Value\"}", "Second element"] 

Is there a quick way to do this?

3
  • What does the java class definition look like for the case with json objects? Your question just has details as ArrayList<String>. Oh I guess you are trying to make a list of strings that contain escaped json. Commented Jul 11, 2020 at 1:11
  • 1
    Take a look at: How deserialize json object array as array of json strings? Commented Jul 11, 2020 at 8:14
  • 1
    Thanks @MichałZiober! I solved it by implementing a custom deserializer. Commented Jul 14, 2020 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.