0

[{"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 DefaultClientConfig();
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

2
  • Can you show us actual code where you use this? Commented Feb 25, 2011 at 22:18
  • See above. I use jersey so there is no real jackson code as i let the provider handle it for me. Thanks Commented Feb 25, 2011 at 22:30

1 Answer 1

3

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick response. I changed the List <Tag> tags to Tag []tags and i got the same error.
yeah sorry about the code. I corrected. I understand that i'm getting a JSON array so i guess my question is how do I define a java object so that Jackson will parse the JSON array correctly? Thanks
Should be something like Tag[] tags = response.getEntity(Tag[].class); or List<Tag> tags = response.getEntity(List<Tag>.class);
Tag[] resp = builder.get(Tag[].class) is the correct way. Thanks. List<Tag>.class is not valid syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.