java - 导航到特定的 JSON 节点并使用 ObjectMapper
问题描述
我想使用 Jackson 导航到特定的 JSON,然后通过 ObjectMapper 发送值。如果 JSON 只是一个像这样的简单数组,我目前可以实现这一点:
[
{
"parent": "A",
"child": "B"
},{
"parent": "G",
"child": "K"
}
]
我的 Java 代码看起来像这样,它可以工作。
ObjectMapper mapper = new ObjectMapper();
Row[] row;
row = mapper.readValue(json, Row[].class);
但是当 JSON 像这样嵌套更多时:
{
"data": [
{
"parent": "A",
"child": "B"
},{
"parent": "G",
"child": "K"
}
]
}
我不确定我需要做什么才能达到同样的效果。
该类Row
只是一个标准类,具有 parent 和 child 属性以及它的 getter 和 setter。那里没什么特别的。
解决方案
你应该使用地图。其中数据是映射中的键,对象列表是值。这是您的列表 [] 是值。在您的列表中,您有一个对象。在 java 中...
Map<String, List<Parents>> studentsParent = HashMap<>(); class Parent{ String parent; String child; parent(String parent, String child){
this.parent = parent;
this.child = child; }
} List<Parents> parents = new ArrayList<>();
studentsParent.put (data, Parents.add( Parent p = new Parent("A", "B"))); [
{
"parent": "A",
"child": "B"
},{
"parent": "G",
"child": "K"
}
]
推荐阅读
- ios - 是否可以更改 TableView 和 SearchBar Delegates 的处理顺序?
- facebook - 是否可以从 Facebook Pixel 隐藏引用页面 URL?
- java - 我应该如何从我的数据库中获取所有消息并返回它
- javascript - fs 不下载图像
- php - 获取 Array 数据(序列化数据?)并显示,这段代码运行良好,但是为什么,它是否正确?
- c - 关于涉及 scanf() 的简单 C 程序的错误
- azure - 无法删除已禁用的订阅
- php - 致命错误:未捕获的错误:C:\xampp\htdocs\tutorial\Online_exam 中未定义的常量“登录”
- python - 从 4 个摄像头创建实时 3D 视频流
- flutter - 如何关闭 Android 模拟器的布局检查器?