简体   繁体   中英

How to deserialize JSON array with different types?

I have a JSON array like this:

[
  "0",
  {
    "number": 1,
    "field": "value"
  },
  {
    "number": 2,
    "field": "value"
  }
]

The first element to the array is a string.

Is it possible to deserialize it with FasterXML Jackson?

I know how to do it with different objects.
I need to use @JsonSubTypes (Here is an example https://stackoverflow.com/a/38877862/2564509 )

The problem with this array is that the first element is String type.

Caveat: Your situation is an unfortunate edge case. As such, the solution is likely to be not wonderful.

This works, but is not wonderful:

  1. First, deserialize as a List<Object> . In your case, this will result in a List of three elements; String, LinkedHashMap, and LinkedHashMap
  2. Next, process each element in the array and, process by type; a String will be your String element, A LinkedHashMap will be the representation of your number-field class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM