我正在尝试映射一些 pojo,就像JSON
使用 spring 的杰克逊技术创建 Web 服务一样,但是当我映射一个列表时,我遇到了一个问题,因为JSON
它创建了一个格式错误JSON
,例如:
["idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2",
"name":"line1",
"code":"pru1"
]
我想创建 a mapper
、 afilter
或类似的东西以获得如下响应:
{
status:"OK",
data:[
{"idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2",
"name":"line1",
"code":"pru1"
}]
}
我尝试了一些ObjectMapper
但没有结果。谢谢。
UPDATE 这一行是解析和调用格式错误的异常的行(它属于外部库)
result = (JSONObject) new JSONTokener(str).nextValue();
我试图解析的 JSON 是:
[{"idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2","name":"linea1","code":"pru1","colour":"#ff3200","means":"Bus","type":"urban","notes":"","state":true,"timetableInterurbans":[]}]
为了澄清,我在 Spring 上使用 @ResponseBody 序列化 Pojo,然后由 android 检索响应并使用引发异常的 JSONObject 解析。
服务器端方法:
@RequestMapping(value="/{country}/{cityName}/{type}", method=RequestMethod.GET)
public @ResponseBody List<Line> getUrbanLinesFromCity(@PathVariable String country, @PathVariable String cityName, @PathVariable String type){
//Get the city pojo by country and city name
City city = cityManager.searchCityByCountryAndName(country, cityName);
//Get the lines from the city
List<Line> lines = null;
if (city != null){
lines = lineManager.getLinesByCityAndType(city.getIdCity(), type);
}
return lines;
}