What if the JSON data actually contains an object and not a string as expected, how to handle this error?

Responsive Ad Header

Question

Grade: Education Subject: Support
What if the JSON data actually contains an object and not a string as expected, how to handle this error?
Asked by:
105 Viewed 105 Answers

Answer (105)

Best Answer
(478)
If the JSON contains an object where you've declared a String, you'll need to modify either the JSON or the Java class. If the JSON is outside of your control, and the object must be handled, change the field type in your Java class to match. Use a complex object, like a Map if the object structure isn't pre-defined or a custom class. If the JSON is correct and the object is expected, you need to create the corresponding Java class and change the field type.