1

在我的应用程序中,我将 JSON 内容作为 InputStream。取决于我想要执行不同操作的单个 JSONObject 还是 JSONArray。

如果它是单个对象或对象数组,我如何区分,使用杰克逊?

//欢呼

解决方案:

使用 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
    }
4

1 回答 1

2

只需绑定为任何一个java.lang.Object(看看你是否有一个ListMap);or as JsonNodeand call isObject()or isArray()on呢?

于 2012-06-15T15:20:20.327 回答