Tag: jackson

使用Jackson反序列化非字符串映射键

我有一个看起来像这样的地图: public class VerbResult { @JsonProperty(“similarVerbs”) private Map<Verb, List> similarVerbs; } 我的动词类看起来像这样: public class Verb extends Word { @JsonCreator public Verb(@JsonProperty(“start”) int start, @JsonProperty(“length”) int length, @JsonProperty(“type”) String type, @JsonProperty(“value”) VerbInfo value) { super(length, length, type, value); } //… } 我想序列化和反序列化我的VerbResult类的实例,但是当我这样做时,我得到这个错误: Can not find a (Map) Key deserializer for type [simple type, class my.package.Verb] 我在网上看到你需要告诉jackson如何反序列化地图密钥,但我没有找到任何解释如何进行此操作的信息。 […]

Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

我在Jersey应用程序中使用Jackson进行JSON序列化/反序列化。 我想在我的java POJO属性中将JSON中的空字符串读取为null值。 我试图在Object Mapper上设置DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT但它不起作用。 这是下面的代码 import java.io.IOException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class TestMapper { /** * @param args */ public static void main(String[] args) { TestMapper.testJson(); } public static void testJson(){ String jsonString = “{\”name\”:\”First Name\”,\”phone\”:\”\”,\”unknown\”:\”test\”}”; ObjectMapper result = new ObjectMapper(); //result.setDeserializationConfig(result.getDeserializationConfig().with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)); //result.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); /*DeserializationConfig deserializeConfig = result.getDeserializationConfig(); deserializeConfig.with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);*/ result.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, […]

jackson在反序列化时抛出JsonMappingException; 要求单串构造函数?

另一个问题,但它涉及到这个问题: 用Jackson反序列化JSON – 为什么JsonMappingException“没有合适的构造函数”? 这次我得到了一个不同的错误,即Jackson反序列化器抱怨我的类ProtocolContainer中没有“单字符串构造函数/工厂方法”。 但是,如果我添加一个单字符串构造函数,如下所示: public ProtocolContainer(String json) {} exception确实消失了,但我希望在那里的ProtocolContainer都是“空”,即它的所有属性都处于初始状态,而不是根据JSON字符串填充。 这是为什么? 我很确定你不需要单一的String构造函数,如果你这样做,你就不必填充那个构造函数中的属性,对吧? =)

jackson – 递归解析为Map

我正在尝试简化我的代码:我想存储键和值(所有字符串)。 我实际上使用Map来存储它。 hat way Object可以是值( String )或新节点( Map )。 我怎么能简化这段代码? 递归函数会很好。 try { JsonParser jsonParser = new JsonFactory().createJsonParser(content); jsonParser.nextToken(); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { jsonParser.nextToken(); if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String key = jsonParser.getCurrentName(); jsonParser.nextToken(); if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { mData.put(key, new HashMap()); while (jsonParser.nextToken() != JsonToken.END_OBJECT) { String subkey […]

如何反映jackson在几秒钟内的时间戳?

我有一些JSON在几秒钟内有时间戳(即Unix时间戳): {“foo”:”bar”,”timestamp”:1386280997} 要求jackson将其反序列化为具有时间戳的DateTime字段的对象,结果是1970-01-17T01:11:25.983Z ,这是纪元之后不久的一段时间,因为jackson认为它是以毫秒为单位 。 除了撕掉JSON并添加一些零之外,我怎样才能让jackson了解秒时间戳?

Spring @JsonIgnore无法正常工作

如何让@JsonIgnore工作我有一堂课。 即使我把注释放在那里它对输出也没有影响。 我在用jackson。 public class QuestionBlock implements ComparableByID{ int ID; String title; String description; boolean deleted; boolean isDraft; boolean visible; Timestamp modifiedDate; String modifiedBy; private List questions = new ArrayList(); @JsonIgnore private List surveys = new ArrayList(); … @JsonIgnore public List getSurveys() { return surveys; } @JsonIgnore public void setSurveys(List surveys) { this.surveys = surveys; […]

如何使用Jackson重命名JSON序列化中的根密钥

我正在使用Jackson进行JSON序列化对象列表。 这是我得到的: {“ArrayList”:[{“id”:1,”name”:”test name”}]} 但我想要这个: {“rootname”:[{“id”:1,”name”:”test name”}]} // ie showing the string I want as the root name. 以下是我的方法: 接口: public interface MyInterface { public long getId(); public String getName(); } 实施class: @JsonRootName(value = “rootname”) public class MyImpl implements MyInterface { private final long id; private String name; public MyImpl(final long id,final name) { this.id […]

JPA瞬态注释和JSON

这是关于JPA瞬态注释的以下问题的后续操作为什么JPA有@Transient注释? 我有一个我不想坚持的瞬态变量,它标有瞬态注释。 但是,当我想从我的其余控制器生成JSON时,此瞬态变量在输出的JSON中不可用。 POJO PublicationVO是直接的,没有花哨的属性,只有一些私有属性(持久化)与getter和setter以及1个瞬态变量。 @RequestMapping(value = { “{publicationId}”}, method = RequestMethod.GET, produces = “application/json”) @ResponseBody public PublicationVO getPublicationDetailsJSON(@PathVariable(value = “publicationId”) Integer publicationId) { LOG.info(“Entered getPublicationDetailsJSON – publicationId: ” + publicationId); //Call method to get the publicationVO based on publicationId PublicationVO publicationVO = publicationServices.getPublicationByIdForRestCalls(publicationId); LOG.info(“publicationVO:{}”, publicationVO); LOG.info(“Exiting getPublicationDetailsJSON”); return publicationVO; } PublicationVO如下 package com.trinity.domain.dao; import […]

反序列化时忽略属性

我有一个简单的接口,带有getter和setter属性。 public interface HasMoney { Money getMoney(); void setMoney(Money money); } 我有另一个类UserAccount,它实现了这个接口。 public class UserAccount implements HasMoney { private Money money; @Override Money getMoney() // fill in the blanks @Override void setMoney(Money money) // fill in the blanks } 我的问题是我想序列化money属性但在反序列化时忽略它,即不接受用户对此属性的任何值。 我在setter上尝试了@JsonIgnore,在getter上尝试了@JsonIgnore(false),它确实忽略了它,但它也在序列化它时也这样做了。 我在setter上尝试了@JsonIgnore,在getter上尝试了@JsonProperty,只是为了明确告诉Jackson我们打算跟踪这个属性,当money属性被发送到服务器并且Jackson试图反序化它抛出MalformedJsonException时,这似乎会崩溃应用程序:不能构造Money类型的对象。 最奇怪的是将@JsonIgnore放在setter上并且setter上的@JsonProperty适用于大多数情况下属性是原始的。

Spring的Json没有得到适当的回应

我试图让Spring中的控制器返回一个JSON响应无法使用3.0推荐的Jackson类。 我当然在我的class级路径中获得了jackson jar文件(jackson-core-asl-1.5.5.jar和jackson-mapper-asl-1.5.5.jar)。 至于appconfig.xml条目,我不确定我是否需要这些。 我把它们放在那里作为绝望的最后一幕,然后回到了’时尚非json ajax’。 在调试中,我看到控制器获取请求,返回foo,然后在firebug中获得406。 错误消息如下:从记录器设置为debug时:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示 从响应:(406)该请求标识的资源仅能够根据请求“accept”headers()生成具有不可接受特性的响应。 我的appconfig.xml在这里: 我的控制器 @RequestMapping(value=”foo/bar”, method=RequestMethod.GET) public @ResponseBody foo getFoo(@RequestParam String fooId) { return new foo(fooId); } 在jsp上,进行ajax调用: function addRow() { $.getJSON(“foo/bar”,{ fooId: 1} , function(data) { alert(“it worked.”); }); } 如果还有其他需要的信息,请告诉我。