-1

这就是我所拥有的。我正在尝试反序列化一个 json,但它不起作用:

public class MyClass {
    private Object attribute;

    @JsonCreator
    public MyClass(Object attribute) {
        this.attribute = attribute;
    }

    @JsonProperty("attr")
    public Object getAttribute() {
        return attribute;
    }
}
public void method() {
  InputStream eventsStream = this.getClass().getClassLoader()
                .getResourceAsStream("fileName.json");

  ObjectMapper objectMapper = new ObjectMapper();
  objectMapper.readValue(eventsStream, MyClass.class);
}



fileName.json
{
  "value1": 1,
  "value2": [
    {
      "subValue1": "valueExample"
    }
  ]
}

我用符号“=”而不是“:”得到这个结果:

{value1=1, value2=[{subValue=valueExample}]}

它必须是一个属性或类似的东西。任何想法?谢谢

4

1 回答 1

0

为什么不将反序列化保存到变量中?

MyClass myClassObject = objectMapper.readValue(eventsStream, MyClass.class);

如果这样做,您将直接将反序列化的对象放入myClassObject其中,因此您可以轻松使用它。另一种反序列化的方法是使用 Gson 来实现,例如:

new Gson().fromJson(eventStream.toString(), MyClass.class); //you need the eventStream as a String type

进口是这个:

import com.google.gson.Gson;

您能告诉我们为什么需要打印反序列化的值吗?

于 2020-12-21T22:12:02.703 回答