-1

I'm here asking for your help after one week of headache :S

I need to deserialize this JSON Output using Jackson

{
   "data": [
      {
         "id": "142065955831788",
         "name": "Name1",
         "link": "http://www.somelink.com",
      },
      {
         "id": "160212467350470",
         "name": "Name2",
         "link": "http://www.somelink2.com",
      }
      .
      .
      .
}

I swear i've tried anything but I just can't make jackson deserialize that JSON.

What am I missing?

EDIT:

I've created a class like this:

Data.class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Data{

    @JsonProperty("data")
    private String name;

    @JsonProperty("data")
    private String link;

//Getters + Setters

And this is my code for the mapping

ObjectMapper mObjectMapper = new ObjectMapper();

        ArrayList<Data> mDataList;

        mDataList = mObjectMapper.readValue(
                url, /* The url returning the JSON */
                mObjectMapper.getTypeFactory().constructCollectionType(
                        ArrayList.class, Data.class
                )
        );

And i get this error message:

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

6
  • Can you pls share your effort ? Commented Oct 9, 2014 at 13:54
  • "What am I missing?" : the error message maybe ? Commented Oct 9, 2014 at 13:54
  • this may help you to understand how to deserialize stackoverflow.com/questions/19109719/… Commented Oct 9, 2014 at 13:56
  • Updated the OP. Sorry for before Commented Oct 9, 2014 at 14:06
  • Does it work if your remove the block "data": [ in your json ? Commented Oct 9, 2014 at 14:13

1 Answer 1

0

Here is the code -

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
class data {

    @JsonProperty("id")
    private String id ;

    @JsonProperty("name")
    private String name ;

    @JsonProperty("link")
    private String link;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

}

public class aa {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();

        try {

            data[] d = mapper.readValue(
                            "[{\"id\":\"142065955831788\",\"name\":\"Name1\",\"link\":\"http://www.somelink.com\"},{\"id\":\"160212467350470\",\"name\":\"Name2\",\"link\":\"http://www.somelink2.com\"}]",
                            data[].class);

            for (data data1 : d) {
                System.out.println("-----");
                System.out.println("Id : "+data1.getId());
                System.out.println("Name : "+data1.getName());
                System.out.println("Link : "+data1.getLink());
            }


        } catch (IOException e) {

            e.printStackTrace();

        }

    }
}

Your JSON also need to be changed. It should not contain data part as you are using array of its properties.

Output -

-----
Id : 142065955831788
Name : Name1
Link : http://www.somelink.com
-----
Id : 160212467350470
Name : Name2
Link : http://www.somelink2.com
Sign up to request clarification or add additional context in comments.

2 Comments

That code works if I dont have te data [] part in my JSON, but I can't modify the JSON response :(
You have array of data right..in your question there is [ in JSON...Also if array is not there you can get single object using data d = mapper.readValue( "{\"id\":\"142065955831788\",\"name\":\"Name1\",\"link\":\"http://www.somelink.com\"}", data.class);

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.