2

We have a JSON-answer from server, where we have array of byte arrays, but there is also can be a "None" string value instead of empty array. Example:

{'jsonrpc': '2.0', 'id': 31, 'result': {"bytes_arrays": [[21,99,165,243,25,210,14,121,120,39,22,102,59],[22,32,42,54,65,65,76,87],None]}

In my class I usually write something like this:

@JsonProperty("bytes_arrays")
    private List<byte[]> mArraysList = new ArrayList<>();

but of course, we'll have a parsing error for the last element with "None" value, because it's a string.

Are there any ways to extract multi-type field in this JSON? We use Jackson.

3
  • Possible duplicate of Deserializing JSON with multiple types in one field Commented Aug 1, 2016 at 13:03
  • What you show here is not valid JSON. Commented Aug 1, 2016 at 13:08
  • so, I'm missed a brace, not server returned Commented Aug 1, 2016 at 13:28

1 Answer 1

1

There was a missing bracket at the end of your JSON string. Fixed and formatted it looks like this:

{
    'jsonrpc' : '2.0',
    'id' : 31,
    'result' : {
        "bytes_arrays" : [ 
                           [ 21, 99, 165, 243, 25, 210, 14, 121, 120, 39, 22, 102, 59 ], 
                           [ 22, 32, 42, 54, 65, 65, 76, 87 ], 
                           None 
                         ]
    }
}

Jackson should be able to parse it into a Map<String, Object>.

You can then check the type of Object with instanceof and put together your logic.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It's worked for me. In my clause, Jackson created a List of LinkedHashMap!

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.