Sampo Kaarenmaa
Sampo Kaarenmaa

Reputation: 49

How to properly map JSON object?

Here is my code:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ObjectMapperJSON {
 public static void main(String[] args) throws IOException, JsonParseException, JsonMappingException {

      ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      String jsonString = "{\"numberOFiles\":13, \"filename\":\"123.jpg\"}";

    /*  int temp = Integer.parseInt(jsonString); 
    */  ObjectMapper objectMapper = getObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

      try{
         JsonBean JsonBean = mapper.readValue(jsonString, JsonBean.class);

         System.out.println(JsonBean);

         jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(JsonBean);
         //jsonString = mapper.writeValueAsString(JsonBean);

         System.out.println(jsonString);
      }
      catch (Exception e){
          e.printStackTrace();}
   }
 }

The outprint from this is: Filename [ filename: 123.jpg, numberOfFiles: 0 ] { "filename" : "123.jpg", "numberOfFiles" : 0

So my question is why the value of numberOfFiles is printed out as 0? Debugging shows me the value until the last syso as 13. How to properly do this?

Upvotes: 0

Views: 60

Answers (1)

Radu Diță
Radu Diță

Reputation: 14201

You have a typo in your json string. Update numberOFiles to numberOfFiles.

Upvotes: 1

Related Questions