如何使用 jackson(Java) 反序列化对象中的 json 对象(在我的例子中,对象Offer存在于current字段中)?
输入字符串:
message.getMessage();
{"header":"OFFER","message":"{\"author\":\"Peter Smith\",\"previous\":null,\"current\":{\"id\":\ "eOUQieQdvB\",\"authorUserId\":\"foo\"}}"}
ObjectMapper mapper = new ObjectMapper();
PushEventMessage<PushEvent<Offer>> pushEventMessage = mapper.readValue(message.getMessage(), PushEventMessage.class);
pushEventMessage.getMessage();
{"author":"Peter Smith","previous":null,"current":{"id":"eOUQieQdvB","authorUserId":"foo"}}
PushEvent<Offer> pushEvent = mapper.readValue(pushEventMessage.getMessage(), PushEvent.class);
pushEvent.getAuthor(); // is OK and contain "Peter Smith"
pushEvent.getCurrent() // is KO and contain {id=eOUQieQdvB, authorUserId=foo}
我想反序列化:
Offer offer= mapper.readValue(pushEvent.getCurrent() + "", Offer.class);
我的错误是:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name
at [Source: (String)"{id=eOUQieQdvB, authorUserId=foo,
编辑 1,我添加PushEvent<T>类。
import lombok.*;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PushEvent<T> {
String author;
T previous;
T current;
String error;
}
编辑 2,我试试这个,但结果是一样的
PushEvent<Offer> pushEvent = mapper.readValue(pushEventMessage.getMessage().replaceAll("\\\"", "\""), PushEvent.class);
我采用@Smutje 的解决方案,这解决了第一个问题。!!
编辑 3,我有一个java.time.ZonedDateTime对象Offer。
{"author":"Peter Smith","previous":null,"current":{"id":"00Yno9WwsL","authorUserId":"foo","createdAt":{"offset":{"totalSeconds" :0,"id":"Z","rules":{"transitionRules":[],"transitions":[],"fixedOffset":true}},"zone":{"id":"UTC" “规则”:{“transitionRules”:[],“transitions”:[],“fixedOffset”:true}},“dayOfMonth”:11,“dayOfWeek”:“SUNDAY”,“dayOfYear”:42,“month” ":"二月","年":2018,"小时":1,"分钟":0,"纳米":0,"秒":0,"月值":2,"年表":{"id" :“国际标准化组织”,“日历类型":"iso8601"}}}}
达令说
白衣染霜花
相关分类