仅反序列化JSON对象数组(Java)的某些字段
鉴于我从一个网站得到的JSON响应:
{
"Items":
[
{ "Name":"Apple", "Price":12.3, "Quantity":30 },
{ "Name":"Grape", "Price":3.21, "Quantity":60 }
],
"Date":"21/11/2010"
}
我如何反序列化这个JSON,将它拆分成一个名为Fruits的数组,只包含名称和数量?我不关心日期字段或其他字段,如价格
我的班级应该是这样的:
class Fruit{
String name;
String quantity;
}
这是阵列:
Fruit myfruits[] = new Fruit [this number depends on JSON response I get]
我怎样才能做到这一点? 我已经尽力给出了最好的解释,如果还不清楚,请尽管问
顺便说一句,真正的JSON响应有更多的字段
# 1 楼答案
你需要忽略那些你不习惯的字段
每个序列化框架都以不同的方式实现
在某些情况下,您可以向
POJO
添加注释,或使用序列化程序实例进行设置使用
Gson
:Gson ignore json field and deserialize
使用
Jackson
:Ignoring new fields on JSON objects using Jackson