gpt4 book ai didi

android - 使用 jackson 在android中使用相同的 key 但不同类型反序列化json

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:28 25 4
gpt4 key购买 nike

我正在调用 Web 服务,它可以有 2 种类型的 json 对象作为响应。现在有时我会得到类型为 String 的 key profile ,有时它可能具有类型为“ProfileSubObject”的相同 key 。那么如何处理这种情况呢?下面是我的两种类型的对象。我正在使用 Jackson 库来解析 json。

1.)

{
"data": [
{
"profession": "iOS Developer",
"thanks": {
"count": 5
},
"profile": "test"
}
]
}

2.)

{
"data": [
{
"profession": "iOS Developer",
"thanks": {
"count": 5
},
"profile": {
"val1":"test1",
"val2":"test2"
}
}
]
}

Key profile 有 2 种不同类型的基于网络服务调用的对象。

以下是我的数据类结构。

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataObject {

@JsonProperty("profession")
private String profession;

@JsonProperty("profile")
private ProfileObject profile;

@JsonProperty("thanks")
private ThanksObject thanks;

public String getProfession() {
return profession;
}

public ThanksObject getThanks() {
return thanks;
}
public ProfileObject getProfile() {
return profile;
}

}

Profile 类如下所示。

public class ProfileObject {

ProfileObject(){

}

ProfileObject(ProfileSubObject profileSubObject){
this.profileSubObject= profileSubObject;

}

ProfileObject(String profile){
this.profile= profile;
}
private ProfileSubObject profileSubObject;
private String profile;

public ProfileSubObject getProfileSubObject() {
return profileSubObject;
}
}

现在,当我解析我的对象时,ProfileObject 始终为 null。我希望它根据 proifle 关键数据类型进行解析。

谁能帮我解析一下?

最佳答案

在构建解决方案时,我遇到了两个问题:

  1. Json 结构不匹配单个DataObject
  2. 将相同属性反序列化为不同类型的 Java 对象的原始问题。

我通过构建 JavaType 对象解决了第一个问题,该对象告诉 Jackson 所涉及集合的通用类型。有两个这样的集合:一个 Map,由带有键 "data" 的单个条目和 DataObject< 的 List 的值组成s

第二个问题,我用 @JsonAnySetter 的 Jackson 功能解决了,它指示 Jackson 为它无法识别的所有属性调用一个方法。为此,我将 @JsonIgnore 添加到配置文件变量中,以确保 Jackson 确实无法识别它。现在 Jackson 为两个输入 json 调用了same方法

这是新的 DataObject 类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DataObject
{
@JsonProperty("profession")
public String profession;

@JsonIgnore // forcing jackson to not recognize this property
public ProfileObject profile;

@JsonProperty("thanks")
public ThanksObject thanks;

public String getProfession() { return profession; }
public void setProfession(String p) { profession = p; }

public ThanksObject getThanks() { return thanks; }
public void setThanks(ThanksObject t) { thanks = t; }

public ProfileObject getProfile() { return profile; }
public void setProfile(ProfileObject p) { profile = p; }

@JsonAnySetter
public void setProfileFromJson(String name, Object value)
{
// if value is single String, call appropriate ctor
if (value instanceof String) {
profile = new ProfileObject((String)value);
}
// if value is map, it must contain 'val1', 'val2' entries
if (value instanceof Map) {
ProfileSubObject profileSubObject =
new ProfileSubObject(((Map<String, String>)value).get("val1"), ((Map<String, String>)value).get("val2"));
profile = new ProfileObject(profileSubObject);
}
// error?
}
}

这是我的测试方法,其中包括我提到的java类型构造:

public static void main(String[] args)
{
try (Reader reader = new FileReader("C://Temp/xx2.json")) {
ObjectMapper mapper = new ObjectMapper();
// type of key of map is String
JavaType stringType = TypeFactory.defaultInstance().constructType(String.class);
// type of value of map is list of DataObjects
JavaType listOfDataObject = TypeFactory.defaultInstance().constructCollectionType(List.class, DataObject.class);
// finally, construct map type with key and value types
JavaType rootMap = TypeFactory.defaultInstance().constructMapType(HashMap.class, stringType, listOfDataObject);

Map<String ,List<DataObject>> m = mapper.readValue(reader, rootMap);

DataObject do1 = m.values()
// get first (only?) value in map (it is list)
.stream().findFirst().orElse(Collections.emptyList())
// get first (only?) item in list - it is the DataObject
.stream().findFirst().orElse(null);

System.out.println(do1.profile);
System.out.println(do1.profile.profile);
System.out.println(do1.profile.profileSubObject.val1 + " " + do1.profile.profileSubObject.val2);
} catch (Exception e) {
e.printStackTrace();
}
}

关于android - 使用 jackson 在android中使用相同的 key 但不同类型反序列化json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42670341/

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