0
我有此代碼傑克遜:與陣列無效JSON形式對象
彈簧的context.xml
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
JsonResponse.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.globe.egg.ggrocery.item.model;
/**
*
* @author Farhan Sharief
*/
public class JsonResponse {
private int status;
private Object body;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Object getBody() {
return body;
}
public void setBody(Object body) {
this.body = body;
}
public static JsonResponse createResponse(int status, Object body) {
JsonResponse response = new JsonResponse();
response.setStatus(status);
response.setBody(body);
return response;
}
}
和我的控制器
@RequestMapping(value = "/list",
method = RequestMethod.GET)
public @ResponseBody JsonResponse getCategories() {
return JsonResponse.createResponse(SUCCESS.getStatus(), categoryService.getCategories()); //returns a List<Category>
}
我在等待
{
"status": 200,
"body": [
{
"categoryId": 1,
"categoryName": "basket",
"subCategory": "NONE",
"version": 1
},
{
"categoryId": 2,
"categoryName": "Cosmetics",
"subCategory": "NONE",
"version": 1
}
]
}
,但我發現
{
status: 200
body: [2]
0: {
categoryId: 1
categoryName: "basket"
subCategory: "NONE"
version: 1
}
1: {
categoryId: 2
categoryName: "Cosmetics"
subCategory: "NONE"
version: 1
}
}
我的問題是,JSON有沒有雙引號和身體結構是無效的。它有[2]。