无法将json放入自定义Java对象列表中
我正在从文件中读取JSON,并且在尝试将文件删除到Java对象时,我不会得到自定义Java对象的预期数组,但是,获取linkedhashmap的数组,
请参阅下面的对象,
public class Result<T>{
private final Map<String, T> data = new LinkedHashMap<>();
public Map<String, T> getAccounts(){
return accounts;
}
}
JSON ->
{
"data":{
"account":[
{
"accountDetails":{
"accountId":"123",
"accountType":"Decon"
}
},
{
"accountDetails":{
"accountId":"890",
"accountType":"ACX"
}
},
{
"accountDetails":{
"accountId":"123",
"accountType":"OOOP"
}
}
]
}
}
@Getter
@Setter
@ToString
public class Accounts{
@Getter
@Setter
public static class AcountDetails{
private String accountId;
private String accountType;
}
}
我试图读取此json,如下所示。
String accounts = Resource.asByteSource(Resources.getResource("account.json")).asCharSource(Charsets.UTF_8).read();
ObjectMapper mapper = new ObjectMapper();
Result<List<Accounts> finalResult = mapper.readValue(accounts, Result.class);
In finalResult variable ,
I am getting a map with key as "account" and value as list
But, instead of List of "Accounts" object, I am getting list of **LinkedHashMap**
因此,在解析后基于基础,而不是获取帐户对象数组,我会收到linkedhashmap数组
,请找到附带的屏幕快照。请指教
I am reading JSON from a file, and when trying to unmarshal the file to a java object, I am not getting expected array of custom Java object, however, getting array of LinkedHashMap
Please see below objects
public class Result<T>{
private final Map<String, T> data = new LinkedHashMap<>();
public Map<String, T> getAccounts(){
return accounts;
}
}
JSON ->
{
"data":{
"account":[
{
"accountDetails":{
"accountId":"123",
"accountType":"Decon"
}
},
{
"accountDetails":{
"accountId":"890",
"accountType":"ACX"
}
},
{
"accountDetails":{
"accountId":"123",
"accountType":"OOOP"
}
}
]
}
}
@Getter
@Setter
@ToString
public class Accounts{
@Getter
@Setter
public static class AcountDetails{
private String accountId;
private String accountType;
}
}
I am trying to read this Json as below
String accounts = Resource.asByteSource(Resources.getResource("account.json")).asCharSource(Charsets.UTF_8).read();
ObjectMapper mapper = new ObjectMapper();
Result<List<Accounts> finalResult = mapper.readValue(accounts, Result.class);
In finalResult variable ,
I am getting a map with key as "account" and value as list
But, instead of List of "Accounts" object, I am getting list of **LinkedHashMap**
So bascially after parsing, Instead of getting array of Accounts objects, I am getting array of LinkedHashMap
Please find attached screenshot. Please advise
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您试图获取一系列对象,那么您的JSON将必须看起来像这样:
当您像这样定义另一个对象时:
您正在创建一个包含两个对象的对象的列表。在Java中,这是一张地图。
list&lt; map&lt; accountId,accountType&gt; gt;
希望它有助于
编辑以进行格式化目的,如果“ element1”是另一个对象:
If you are trying to get an Array of objects, your JSON would have to look something like this:
When you are defining another object as you did:
You are creating a List of an Object that contains two more Objects. In Java that's a Map.
List<Map<AccountId, AccountType>
Hope it helps
Edit for formatting purposes if "Element1" were another Object:
首先,您可以在这里确认您想做什么:
其次,您的JSON具有具有一个帐户的“数据”:[]对,因此先从数据中首先读取值,然后从帐户中读取值,您将获得列表。
First of all can you confirm what you want to do here:
Secondly, your json has 'data' which has a account: [] pair, so first read value from data, then from account and you will get the list.
它在更改
为
It worked after changing
to