exmaple.file
我想将以下json映射到object。我使用杰克逊尝试过自定义对象。
[
{
"widgetTitle":"Bannerfgfg",
"widgetCode" : "PRTIMG001",
"widgetType":"dssdsd",
"widGetSpanNumber":"2",
"jqimagePath":"images/slider/",
"jqimageCount":"4",
"jqimageHeight":"300"
},
{
"widgetTitle" : "Favourites",
"widgetCode" : "Favourites",
"widgetType" : "sdsdsd",
"widgetAttributes":{
{"key":"loadTemplate","value":[{"url" : "favourites"}]
},
{"key":"loadCntId","value": "tobeloaded"}
}
},
{
"widgetTitle" : "Exchange Rates",
"widgetCode" : "sdsdsd",
"widgetType" : "sdsdsdsdsd",
"widgetAttributes": {
{"key":"datatype","value": "json"},
{"key":"appCode","value": "mst"},
{"key":"url","value":"exchangeMaster/getCurrencyExchangeDetail?"},
{"key":"colNames" ,"value":["Form CCY","To CCY","Ex RT","Prd FRM","Prd TO"]},
{"key":"colModel","value":[ {"name" : "fromCurrencyCode","index" : "fromCurrencyCode","align":"center"},
{"name" : "toCurrencyCode","index" : "toCurrencyCode","align":"center"},
{"name" : "exchangeRate", "index" : "exchangeRate","align":"right"},
{"name" : "periodFrom","index" : "periodFrom"},
{"name" : "periodTo","index" : "periodTo"}
]},
{"key":"width","value": "440"},
{"key":"sortable","value": true},
{"key": "filterToolbar","value":{
"stringResult": "false",
"searchOnEnter": "true",
"enableClear": "false"
}},
{"key":"jsonReader","value":{
"root":"currencyExchangeSOs"
}},
{"key":"sortname","value": "fromCurrencyCode"}
}
}
]
我使用以下代码将json映射到对象
ObjectMapper mapper = new ObjectMapper();
InputStream src = resource.getInputStream();
try {
PortalModal[] portalModal = mapper.readValue(src, PortalModal[].class);
portalDataList = Arrays.asList(portalModal);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
PortalModal.java
public class PortalModal {
private String widgetCode;
private String widgetTitle;
private String widgetType;
private Boolean isAuthorize;
private Map<String,Object> widgetAttributes;
private String widGetSpanNumber;
private String jqimagePath;
private String jqimageCount;
private String jqimageHeight;
private HashMap<String,String> widgetContent;
public String getWidgetCode() {
return widgetCode;
}
public void setWidgetCode(String widgetCode) {
this.widgetCode = widgetCode;
}
public String getWidgetTitle() {
return widgetTitle;
}
public void setWidgetTitle(String widgetTitle) {
this.widgetTitle = widgetTitle;
}
public String getWidgetType() {
return widgetType;
}
public void setWidgetType(String widgetType) {
this.widgetType = widgetType;
}
public Boolean getIsAuthorize() {
return isAuthorize;
}
public void setIsAuthorize(Boolean isAuthorize) {
this.isAuthorize = isAuthorize;
}
public Map<String, Object> getWidgetAttributes() {
return widgetAttributes;
}
public void setWidgetAttributes(Map<String, Object> widgetAttributes) {
this.widgetAttributes = widgetAttributes;
}
public String getWidGetSpanNumber() {
return widGetSpanNumber;
}
public void setWidGetSpanNumber(String widGetSpanNumber) {
this.widGetSpanNumber = widGetSpanNumber;
}
public String getJqimagePath() {
return jqimagePath;
}
public void setJqimagePath(String jqimagePath) {
this.jqimagePath = jqimagePath;
}
public String getJqimageCount() {
return jqimageCount;
}
public void setJqimageCount(String jqimageCount) {
this.jqimageCount = jqimageCount;
}
public String getJqimageHeight() {
return jqimageHeight;
}
public void setJqimageHeight(String jqimageHeight) {
this.jqimageHeight = jqimageHeight;
}
public HashMap<String, String> getWidgetContent() {
return widgetContent;
}
public void setWidgetContent(HashMap<String, String> widgetContent) {
this.widgetContent = widgetContent;
}
}
但是我遇到了以下错误。
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
at [Source: sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@1797b87; line: 9, column: 31] (through reference chain: com.qr.jadu.portal.modal.PortalModal["widgetAttributes"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:246)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:33)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:104)
at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1900)
at com.qr.jadu.portal.core.PortalConfigImpl.initPortal(PortalConfigImpl.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
我不想使用simplejson。请帮我解决这个问题。
答案 0 :(得分:1)
对于窗口小部件属性,您需要为键/值对象创建一个类,并将地图更改为数组。你的新对象看起来像这样:
public class Attribute {
private String key;
private String value;
@JsonCreator
public Attribute(@JsonProperty("key") String key,
@JsonProperty("value") String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
PortalModal
上的您的财产将如下所示:
private List<Attribute> widgetAttributes;
答案 1 :(得分:0)
List<PortalModal> portalDataList;
ObjectMapper mapper = new ObjectMapper();
Map<String, Object>[] maps = mapper.readValue(
resource.getInputStream(), Map[].class);
List<Object> obj1 = new ArrayList<Object>();
for (Map<String, Object> map : maps) {
obj1.add(map);
}
portalDataList = (List<PortalModal>) (List) obj1;
此代码工作正常..