android Jackson json对象映射器数组反序列化
我需要帮助将 Jackson 映射器的响应解析为 POJO。我有这样的回应:
"data": [{
"item": {
"downloaded": false,
"moderated": false,
"add": false
}
},
{
"item": {
"downloaded": false,
"moderated": false,
"add": false }
// more
那么我如何将这个与映射器绑定到 POJO? 这是我正在尝试的课程,但它返回“项目”无法识别且不允许被忽略。
public ArrayList<Item> data = new ArrayList<Item>();
其中 item 是一个带有构造函数的公共静态类 Item,并且上面的所有字段都带有 getter 和 setter。
我该怎么做。我似乎无法在任何地方找到如何以这种方式从数组读取数据。
i need help parsing a response with the jackson mapper to a POJO. i have this as a response:
"data": [{
"item": {
"downloaded": false,
"moderated": false,
"add": false
}
},
{
"item": {
"downloaded": false,
"moderated": false,
"add": false }
// more
so how do i bind this one with the mapper to a POJO?
here is my class that i am trying but it returns that "item" is not recognized and not allowed to be ignored.
public ArrayList<Item> data = new ArrayList<Item>();
where item is a public static class Item with constructors and all the fields above with getters and setters.
how do i do this. i cant seem to find anywhere how to read data from an array this way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例缺少几个部分(尤其是 Item 的定义),无法了解您的结构是否兼容;但一般来说 JSON 和 Object 结构需要匹配。因此,您至少需要类似:
如果是这样,您将绑定:
并按如下方式访问数据
Your example is missing couple of pieces (esp. definition of Item), to know if your structure is compatible; but in general JSON and Object structures need to match. So, you would at least need something like:
and if so, you would bind with:
and access data as
这是我的 maziar 代码的变体。
它只是消除了首先转换为 JsonNode 的过程,而是直接转换为 List;仍然工作正常,输出项目列表。
Here's my variant of maziar's code.
It just eliminates the converting first to a JsonNode and converts instead directly to a List; still works fine, outputting a list of Items.