Nils
Nils

Reputation: 785

How to differentiate JSONArray and JSONObject from InputStream using Jackson?

In my app I get JSON content as InputStream. Depending if its a single JSONObject or a JSONArray of those I want to perform different actions.

How can I differentiate, using Jackson, if it is an single object or an array of objects?

// cheers

SOLUTION:

Using JsonNote.isArray():

JsonNode rootNode = mapper.readValue(contentStream, JsonNode.class);    
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();

    if(rootNode.isArray()){
        // do something with the array

    } else {
        // do something else with the object
    }

Upvotes: 1

Views: 916

Answers (1)

StaxMan
StaxMan

Reputation: 116582

Just bind as either java.lang.Object (and see if you got a List or Map); or as JsonNode and call isObject() or isArray() on it?

Upvotes: 2

Related Questions