我有以下字符串,我收到上述错误。
{product:[{product_type_id:1, product_cat_id:1, product_type_nm:PED}, {product_type_id:2, product_cat_id:2, product_type_nm:MOBILE APP}, {product_type_id:3, product_cat_id:1, product_type_nm:MOBILE}, {product_type_id:4, product_cat_id:3, product_type_nm:PAYMENT}, {product_type_id:5, product_cat_id:5, product_type_nm:USER}, {product_type_id:6, product_cat_id:6, product_type_nm:SMS}, {product_type_id:9, product_cat_id:6, product_type_nm:EMAIL}, {product_type_id:10, product_cat_id:6, product_type_nm:TOPUP}]}
我的String中有什么问题。
以下是我用来转换的代码。
ObjectMapper mapper = new ObjectMapper();
TypeFactory typeFactory = TypeFactory.defaultInstance();
List list = mapper.readValue(new_value, typeFactory.constructCollectionType(List.class, BillingProductServiceMappingDTO.class));
以下是我得到的例外,
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: {product:[{product_type_id:1, product_cat_id:1, product_type_nm:PED}, {product_type_id:2, product_cat_id:2, product_type_nm:MOBILE APP}, {product_type_id:3, product_cat_id:1, product_type_nm:MOBILE}, {product_type_id:4, product_cat_id:3, product_type_nm:PAYMENT}, {product_type_id:5, product_cat_id:5, product_type_nm:USER}, {product_type_id:6, product_cat_id:6, product_type_nm:SMS}, {product_type_id:9, product_cat_id:6, product_type_nm:EMAIL}, {product_type_id:10, product_cat_id:6, product_type_nm:TOPUP}]}; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:853)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:292)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2764)
at com.mkyong.common.dao.BundleDaoImpl.getDetails(BundleDaoImpl.java:64)
at com.mkyong.common.controller.HelloWorldController.handleRequestInternal(HelloWorldController.java:22)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:154)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
以下是我正在使用的bean,
package com.mkyong.common.dto;
import java.io.Serializable;
import java.util.List;
public class BillingProductServiceMappingDTO implements Serializable{
/**
*
*/
private int product_type_id;
private int product_cat_id;
private int product_type_nm;
public int getProduct_type_id() {
return product_type_id;
}
public void setProduct_type_id(int product_type_id) {
this.product_type_id = product_type_id;
}
public int getProduct_cat_id() {
return product_cat_id;
}
public void setProduct_cat_id(int product_cat_id) {
this.product_cat_id = product_cat_id;
}
public int getProduct_type_nm() {
return product_type_nm;
}
public void setProduct_type_nm(int product_type_nm) {
this.product_type_nm = product_type_nm;
}
}
答案 0 :(得分:0)
您需要打开根值(“product”)才能使其正常工作。
将以下属性添加到mapper
对象:
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
答案 1 :(得分:0)
您的Json无效。尝试先验证您的json http://jsonlint.com/
您可以尝试类似
的内容{
"product": [{
"product_type_id": 1,
"product_cat_id": 1,
"product_type_nm": "PED"
}, {
"product_type_id": 2,
"product_cat_id": 2,
"product_type_nm": "MOBILE APP"
}, {
"product_type_id": 3,
"product_cat_id": 1,
"product_type_nm": "MOBILE"
}, {
"product_type_id": 4,
"product_cat_id": 3,
"product_type_nm": "PAYMENT"
}, {
"product_type_id": 5,
"product_cat_id": 5,
"product_type_nm": "USER"
}, {
"product_type_id": 6,
"product_cat_id": 6,
"product_type_nm": "SMS"
}, {
"product_type_id": 9,
"product_cat_id": 6,
"product_type_nm": "EMAIL"
}, {
"product_type_id": 10,
"product_cat_id": 6,
"product_type_nm": "TOPUP"
}]
}