2015-05-13 141 views
0

我使用Grails中的Jackson庫來解析JSON對象,JSON對象中的一個鍵(來自InputStream)是hot_regions,其值是一個JSON對象數組:使用Jackson解析JSON對象數組

String regionsJsonStr = "{ 
"hot_regions" : [ 
{"region_code_1" : "America"}, 
{"region_code_2" : "Europe"}, 
{"region_code_3" : "Asia"} 
] 
}" 

下面是嘗試使用傑克遜解析陣列Java代碼的一部分:

ObjectMapper mapper = new ObjectMapper(); 
JsonNode root = mapper.readTree(regionsJsonStr); 
JsonNode hotRegions = root.path("hot_regions"); 
TypeReference<List<Region>> regionRef = new TypeReference<List<Region>>(){}; 
List<Region> regions = mapper.readValue(hotRegions.traverse(), regionRef); 

區域被定義爲一個內部類:

private class Region { 
    private String regionCode; 
    private String regionName; 

    public Region() {} 
    public Region(String code, String name) { 
     this.regionCode = code; 
     this.regionName = name; 
    } 
} 

這不起作用,Grails報告以下錯誤,我想知道是什麼原因導致錯誤。謝謝

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) 

回答