无法将json放入自定义Java对象列表中

发布于 2025-02-11 14:35:10 字数 1637 浏览 4 评论 0原文

我正在从文件中读取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2025-02-18 14:35:10

如果您试图获取一系列对象,那么您的JSON将必须看起来像这样:

"element": [
{
  "element1": "Value 1",
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}
]

当您像这样定义另一个对象时:

"account":[
       {
           "accountDetails":{
                  "accountId":"123",
                  "accountType":"Decon"
           }
       },

您正在创建一个包含两个对象的对象的列表。在Java中,这是一张地图。
list&lt; map&lt; accountId,accountType&gt; gt;

希望它有助于

编辑以进行格式化目的,如果“ element1”是另一个对象:

"element": [
{
  "element1": {
    "element3": "Value",
    "element4": "Value"
  },
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}]

If you are trying to get an Array of objects, your JSON would have to look something like this:

"element": [
{
  "element1": "Value 1",
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}
]

When you are defining another object as you did:

"account":[
       {
           "accountDetails":{
                  "accountId":"123",
                  "accountType":"Decon"
           }
       },

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:

"element": [
{
  "element1": {
    "element3": "Value",
    "element4": "Value"
  },
  "element2": "Value 1"
},
{
  "element1": "Value 1",
  "element2": "Value 1"
}]
小兔几 2025-02-18 14:35:10

首先,您可以在这里确认您想做什么:

映射最终地图&lt; string,t&gt;帐户= new LinkedHashmap&lt;&gt;();
内部的结果?因为如果是一个变量,请删除前导映射,并且不要将变量作为最终。
另外,您将需要两个固定器和变量的固定器。

其次,您的JSON具有具有一个帐户的“数据”:[]对,因此先从数据中首先读取值,然后从帐户中读取值,您将获得列表。

First of all can you confirm what you want to do here:

Map final Map<String, T> accounts = new LinkedHashMap<>();
inside class Result? Because if it is a variable, then remove the leading Map and don't make the variable as final.
Also you will need both setters and getters for the variable.

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.

它在更改

Result<List<Accounts> finalResult = mapper.readValue(accounts, Result.class);

Result<List<Accounts>> finalResult = mapper.readValue(accounts, new TypeReference<Result<List<Accounts>>>() {});

It worked after changing

Result<List<Accounts> finalResult = mapper.readValue(accounts, Result.class);

to

Result<List<Accounts>> finalResult = mapper.readValue(accounts, new TypeReference<Result<List<Accounts>>>() {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文