使用Jackson解析一组JSON对象

时间:2015-05-13 16:14:16

标签: json grails jackson

我使用Grails中的Jackson库来解析JSON对象,JSON对象中的一个键(来自InputStream)是hot_regions,其值是JSON对象的数组:

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

以下是尝试使用Jackson解析数组的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)

1 个答案:

答案 0 :(得分:-1)

内部课程必须是静态的,以便杰克逊能够使用它们。

http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.html