开发者

How to serialize this JSON Array String using Jackson Annotations?

[{"ID":"hzQ8ll","CreationDate":"Thu, 24 Feb 2011 12:53:31 GMT","Count":6,"Name":"SOMETAG"}]

The inside is of type Tag so I just wrote this Java class:

public class Tags {
    public List <Tag>tags;
}

But I get com.sun.jersey.api.client.ClientHandlerException:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.test.Tags  out of START_ARRAY token

I am using Jersey with the JacksonJsonProvider like this:

ClientConfig config = new DefaultClientCon开发者_高级运维fig();
config.getClasses().add(JacksonJsonProvider.class);

Then I just do a simple Jersey client call:

ClientResponse response = builder.get(ClientResponse.class);
Tags tags = response.getEntity(Tags.class);

Any ideas? Most of the time my outermost elements had a name associated to it so this is new to me. Thanks for any help


You possibly have to declare a Tag[] instead of a List<Tag>. I had a similar issue with a different JSON library. It seems to have to do with difficulties introspecting generic containers.

You have a strange usage of get().

http://jersey.java.net/nonav/apidocs/1.5/jersey/com/sun/jersey/api/client/UniformInterface.html#get%28java.lang.Class%29

Return and argument type should be the same.

Either:

 ClientResponse resp = builder.get(ClientResponse.class);

or

 Tag[] resp = builder.get(Tag[].class);

Anyway, it seems tha the problem is that your JSON data is an array and it is being deserialized into something that is not (Tags).

Try this directly:

Tag[] tags = response.getEntity(Tag[].class); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜