@ResponseBody返回
[{"id":1010,"name":"projectname2"}]类型json字符串
但是我需要一个下面的json字符串
[{"id":1010,"name":"projectname2","age":"21"}]
那么我如何将age属性连接到默认的通用json字符串呢?
我正在使用java spring-mvc框架和spring-json jar
@RequestMapping(value = "/projectsByEmployeeId/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Project> getProjectsByEmployeeId(
HttpServletRequest request) {
String filter = request.getParameter("filter");
FilterAttributes[] filterAttributes = null;
try {
filterAttributes = new ObjectMapper().readValue(filter,
FilterAttributes[].class);
} catch (Exception exception) {
logger.error("Filtering parameter JSON string passing failed",
exception);
}
if ((filterAttributes != null)
&& (!filterAttributes[0].getStringValue().isEmpty())) {
return utilityService.getProjectsByEmployeeId(44L);//this is an example id
} else {
return utilityService.getProjects();
}
}发布于 2013-06-11 21:46:12
实现这一点的另一种方法(您说您现在正在使用JSONObject,而JSONObjects不是默认库)
拿着这根线
[{"id":1010,"name":"projectname2"}]并将其转换为JSONObject。
转换完成后,您可以使用JSONObject.append()将值为"21“的键"age”附加到它的后面。
发布于 2013-06-11 21:56:52
实现这一点的另一种方法是纯字符串操作
String json = "[{\"id\":1010,\"name\":\"projectname2\"}]";
json = json.substring(0, json.length() - 2); //strip the }]
String newJSON = json.concat(",\"age\": 21}]"); // add in the new key and end
System.out.println(newJSON); // [{"id":1010,"name":"projectname2","age": 21}]https://stackoverflow.com/questions/17045184
复制相似问题