JSON数组转换为Spring Data Elasticsearch中的Java对象时获得空值。

huangapple 未分类评论47阅读模式
英文:

JSON Array to Java object in Spring Data Elasticsearch getting Null value

问题

我正在使用版本为2.1.0的spring-data-elasticsearch的Java。

除了customList字段外,其他所有字段都正常工作。在Java中,customList始终获取null值。

我的JSON:

{
    "Id": "123",
    "customList": {
        "Item 1": [
            {
                "name": "xyz",
                "price": "15"
            },
            {
                "name": "qwe",
                "price": "11"
            }
        ],
        "Item 2": [
            {
                "name": "abc",
                "price": "20"
            }
        ]
    }
}

我正在尝试在类A中反序列化此JSON:

@Document(indexName = "repository", type = "sop")
public class A {

    @Id
    private String Id;
    @Field(type = FieldType.Nested)
    private Map<String, List<B>> customList;
}

public class B {
    private String name;
    private String price;
}

A的反序列化对象

{
    "id": "123",
    "customList": null
}

请问是否有人可以解释为什么这个数组没有被反序列化,以及应该如何纠正它。
英文:

I am using Java with spring-data-elasticsearch version 2.1.0

Every field except the customList works fine. customList in java is getting a null value everytime.

My JSON:

{
&quot;Id&quot;: &quot;123&quot;,
&quot;customList&quot;: {
    &quot;Item 1&quot;: [
        {
            &quot;name&quot;: &quot;xyz&quot;,
            &quot;price&quot;: &quot;15&quot;
        },
        {
            &quot;name&quot;: &quot;qwe&quot;,
            &quot;price&quot;: &quot;11&quot;
        }
    ],
    &quot;Item 2&quot;: [
        {
            &quot;name&quot;: &quot;abc&quot;,
            &quot;price&quot;: &quot;20&quot;
        }
    ]
}

I am trying to deserialize this json in class A:

@Document(indexName = &quot;repository&quot;, type = &quot;sop&quot;)
public class A {

  @Id
  private String Id;
  @Field(type = FieldType.Nested)
  private Map&lt;String, List&lt;B&gt;  &gt; customList;
}

public class B {
  private string name;
  private string price;
}

Deserialized object of A

{
  &quot;id&quot;: &quot;123&quot;,
  &quot;List&quot;: null
}

Can anyone please give a reason why this array is not deserializing and what can be done to correct it.

huangapple
  • 本文由 发表于 2020年6月29日 05:56:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/62628657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定