【问题标题】:Deserialize json with same key but different type in android using Jackson使用Jackson在android中反序列化具有相同键但不同类型的json
【发布时间】:2017-07-28 23:05:47
【问题描述】:

我正在调用 Web 服务,它可以有 2 种类型的 json 对象作为响应。现在有时我会得到字符串类型的键profile,有时它可能具有类型为“ProfileSubObject”的相同键。那么如何处理这种情况呢?以下是我的两种类型的对象。我正在使用 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 种不同类型的基于 web 服务调用的对象。

以下是我的数据类结构。

@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 始终为空。我希望它根据proifle 键数据类型进行解析。

谁能帮我解析一下?

【问题讨论】:

  • 你的意思是在"profile": "test"的情况下它是一个子配置文件吗?
  • @Sharon Ben Asher 编辑了我的问题

标签: android json jackson


【解决方案1】:

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

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

我通过构造JavaType 对象解决了第一个问题,这些对象告诉杰克逊所涉及的集合的泛型类型。有两个这样的集合:Map,由单个条目组成,键为 "data",值为 ListDataObjects

第二个问题,我用@JsonAnySetter 的Jackson 特性解决了它,它指示Jackson 为它不识别的所有属性调用一个方法。为此,我将@JsonIgnore 添加到配置文件变量中,以确保杰克逊确实无法识别它。现在 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();
    }
}

【讨论】:

  • 我相信这是假设没有其他附加属性但很好的解释!!
  • 当然解决方案是根据提供的信息量身定制的。您可以通过质疑键和值参数的类型和内容来细化输入
  • @Sharon Ben Asher 非常感谢您的快速解决方案和解释,
【解决方案2】:

这可能有助于解析 JSON,请使用 JsonReader。它确实假设您正在使用 RESTful web 服务并且已经从连接中获得了一个 HttpURLConnection 和一个 InputStream。

https://developer.android.com/reference/android/util/JsonReader.html

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2019-06-21
    相关资源
    最近更新 更多