gpt4 book ai didi

java - 如何使用 Jackson 区分 JSONArray 和 JSONObject 与 InputStream?

转载 作者:行者123 更新时间:2023-11-30 04:45:33 27 4
gpt4 key购买 nike

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

如何使用 Jackson 区分它是单个对象还是对象数组?

//干杯

解决方案:

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

最佳答案

只需绑定(bind)为java.lang.Object(看看您是否有ListMap);或者作为 JsonNode 并调用 isObject()isArray()

关于java - 如何使用 Jackson 区分 JSONArray 和 JSONObject 与 InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11035016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com