0

I am having difficulties transforming a Json String into an Object in java using Jackson.

Model

public class MyList {    
   public List<MyObj> ads;
}

public class MyObj {    
   public String id_ad;
}

Data:

String con = "{\"ads\":[{\"id_ad\":\"20439\"}, {\"id_ad\":\"20449\"}]";

Retrieve code:

ObjectMapper objectMapper = new ObjectMapper();
MyList annonces = objectMapper.readValue(con, MyList.class);

Error:

Erreur dans getAllAds: com.fasterxml.jackson.core.io.JsonEOFException: 
Unexpected end-of-input: expected close marker for Object (start marker at [Source: {"ads":[{"id_ad":"20439"}, {"id_ad":"20449"}]; line: 1, column: 1])
 at [Source: {"ads":[{"id_ad":"20439"}, {"id_ad":"20449"}]; line: 1, column: 91]

Questions:

  1. What is wrong?

  2. Do I need getter/setter for Jackson or public member should work fine?

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
Tyvain
  • 2,640
  • 6
  • 36
  • 70

1 Answers1

1

Your JSON is incorrect, it is missing a closing curly bracket (}) at the end.

Change from:

String con = "{\"ads\":[{\"id_ad\":\"20439\"}, {\"id_ad\":\"20449\"}]";

to:

String con = "{\"ads\":[{\"id_ad\":\"20439\"}, {\"id_ad\":\"20449\"}]}";
marcelovca90
  • 2,673
  • 3
  • 27
  • 34
Pedro Rodrigues
  • 1,662
  • 15
  • 19