Skip to content
Advertisement

Java: FasterXML / jackson deserialize array without keys

Is there a way how to deserialize JSON array

{["a", "b", 1]}

into following Java class

class MyObject {
  private String firstItem;
  private String secondItem;
  private int thirdItem;
}

using FasterXML jackson-databind?

I only found answers where there are key: value items in the array.

Advertisement

Answer

Firstly {["a", "b", 1]} is not a Valid Json Array (or JSON) …. JSON Array would look like this ["a", "b", 1]

Also you could deserialize the Json Array into a Java Object by writing a Custom Deserializer for the Java Object and Register it with Object Mapper using Module in Faster Xml.

Check the Following Link to get more Info on How to write Custom Deserializers

http://www.baeldung.com/jackson-deserialization

Still I would suggest not using Array Representation for an Object.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement