gpt4 book ai didi

java - HashMap 嵌套对象的 GSON Json 映射

转载 作者:行者123 更新时间:2023-12-02 09:03:54 25 4
gpt4 key购买 nike

我有一个如下所示的 json 上下文:

{
"data": {
"details": {
"en-CA": {
"languageCode": "en-CA",
"isPrimaryLocale": false
},
"en-US": {
"languageCode": "en-US",
"isPrimaryLocale": true,
"languageDisplayName": "English (United States)",
}
}
}
}

在java中用GSON映射它:

我创建了这个类:

public class ApiResponseSingleDto
{
private ResponseDetail data;
}

public class ResponseDetail
{
private ResponseDetails details;

@Getter
public static class ResponseDetails
{
public HashMap<String, LocaleDetail> row = new HashMap<>();
}
}

public class LocaleDetail
{
private String languageCode;
private Boolean isPrimaryLocale;
private String languageDisplayName;
}

当我尝试将 json 映射到 Java POJO 类时,HashMap 不起作用。有什么建议吗?

映射它:

GSON.fromJson("...json", Type type...);

最佳答案

尝试替换:

public class ApiResponseSingleDto
{
private ResponseDetail data;
}

public class ResponseDetail
{
private Map<String, LocaleDetail> details;
}

public class LocaleDetail
{
private String languageCode;
private Boolean isPrimaryLocale;
private String languageDisplayName;
}

另外 json 似乎不正确:"languageDisplayName": "English (United States)",

应该只是“languageDisplayName”:“英语(美国)”

还有一点:我相信你应该有公共(public)字段或至少有 setter/getter

关于java - HashMap 嵌套对象的 GSON Json 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59968801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com