title: Using Jackson to parse an array of JSON objects
body: <p>I use the Jackson library in Grails to parse JSON objects, one of the key in the JSON object (from an InputStream) is <code>hot_regions</code>, whose value is an array of JSON objects:</p>
  
  <pre><code>String regionsJsonStr = "{
  "hot_regions" : [
  {"region_code_1" : "America"},
  {"region_code_2" : "Europe"},
  {"region_code_3" : "Asia"}
  ]
  }"
  </code></pre>
  
  <p><strong>Below is part of the Java code that tries to use Jackson to parse the array:</strong></p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  JsonNode root = mapper.readTree(regionsJsonStr);
  JsonNode hotRegions = root.path("hot_regions");
  TypeReference&lt;List&lt;Region&gt;&gt; regionRef = new TypeReference&lt;List&lt;Region&gt;&gt;(){};
  List&lt;Region&gt; regions = mapper.readValue(hotRegions.traverse(), regionRef);
  </code></pre>
  
  <p><strong>Region is defined as an inner class:</strong></p>
  
  <pre><code>private class Region {
      private String regionCode;
      private String regionName;
  
      public Region() {}
      public Region(String code, String name) {
          this.regionCode = code;
          this.regionName = name;
      }
  }
  </code></pre>
  
  <p><strong>This does not work and Grails reports the following errors, I am wondering what causes the errors. Thanks</strong></p>
  
  <pre><code>No suitable constructor found for type [simple type, class com.company.RegionService$Region]: can not instantiate from JSON object (need to add/enable type information?)
   at [Source: N/A; line: -1, column: -1]. Stacktrace follows:
  com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.company.RegionService$Region]: can not instantiate from JSON object (need to add/enable type information?)
   at [Source: N/A; line: -1, column: -1]
      at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:400)
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
      at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:226)
      at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:203)
      at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
      at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2768)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1492)
      at com.company.RegionService.getRegions(RegionService.groovy:48)
      at com.company.SearchController.getRegions(SearchController.groovy:374)
      at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:200)
      at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
      at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
      at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
      at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>