将 JSON 字符串反序列化为具有字符串属性的对象,在 Jackson 中。

huangapple go评论79阅读模式
英文:

Deserialize json string to object with properties which are strings in jackson

问题

我有一个看起来像这样的 JSON 字符串:
```json
{"a":5, "b":"asd", "c":"{\"d\":3}"}

这可以被反序列化为一个对象,就像这样:

class A {
   int a; // --> 5
   String b; // --> 'asd'
   String c; // --> '{\"d\":3}'
}

但我想要它被反序列化为:

class A {
   int a; // --> 5
   String b; // --> 'asd'
   MyClass c; // --> '{\"d\":3}'
}

其中 MyClass 是:

class MyClass {
   int d; // --> 3
}

在 jackson 中,我如何实现这个反序列化?


<details>
<summary>英文:</summary>

I have a json string which looks like:
```{&quot;a&quot;:5, &quot;b&quot;:&quot;asd&quot;, &quot;c&quot;:&quot;{\&quot;d\&quot;:3}&quot;}```
This can be deserialized to an object like:

class A {
int a; // --> 5
String b; // --> 'asd'
String c; // --> '{"d":3}'
}

but i want it to be deserialized as:

class A {
int a; // --> 5
String b; // --> 'asd'
MyClass c; // --> '{"d":3}'
}

where MyClass is:

class MyClass {
int d; // --> 3
}


How can I achieve this in jackson during deserialization?

</details>


# 答案1
**得分**: 2

我刚刚发现我可以使用Jackson转换器:

```java
public class MyClassConverter implements Converter<String, MyClass> {

  @Override
  public MyClass convert(String value) {
    try {
      return new ObjectMapper().readValue(value, MyClass.class);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public JavaType getInputType(TypeFactory typeFactory) {
    return typeFactory.constructSimpleType(String.class, null);
  }

  @Override
  public JavaType getOutputType(TypeFactory typeFactory) {
    return typeFactory.constructSimpleType(MyClass.class, null);
  }

}

在Bean中:

class A {
   int a; 
   String b; 

   @JsonDeserialize(converter = MyClassConverter.class)
   MyClass c; 
}
英文:

I just found out that I can use the jackson converter:

public class MyClassConverter implements Converter&lt;String, MyClass&gt; {

  @Override
  public MyClass convert(String value) {
    try {
      return new ObjectMapper().readValue(value, MyClass.class);
    } catch (JsonProcessingException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public JavaType getInputType(TypeFactory typeFactory) {
    return typeFactory.constructSimpleType(String.class, null);
  }

  @Override
  public JavaType getOutputType(TypeFactory typeFactory) {
    return typeFactory.constructSimpleType(MyClass.class, null);
  }

}

And in the Bean:

class A {
   int a; 
   String b; 

   @JsonDeserialize(converter = MyClassConverter.class)
   MyClass c; 
}

答案2

得分: 0

尝试进行两次反序列化:

A aObject = mapper.readValue(json, A.class);
aObject.setCObject(mapper.readValue(aObject.getC(), C.class));

class A {
    int a;
    String b;
    String c;
    C cObject;
}

class C {
    int d;
}
英文:

Try to do the deserialization twice:

A aObject = mapper.readValue(json,A.class);
aObject.setCObject(mapper.readValue(aObject.getC(),C.class));

class A {
    int a;
    String b;
    String c;
    C cObject;
}

class C {
    int d;
}

huangapple
  • 本文由 发表于 2020年8月24日 15:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556308.html
匿名

发表评论

匿名网友

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

确定