Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不使用gson库的情况下从@ResponseBody创建JsonObject_Java_Json_Spring_Spring Json - Fatal编程技术网

Java 在不使用gson库的情况下从@ResponseBody创建JsonObject

Java 在不使用gson库的情况下从@ResponseBody创建JsonObject,java,json,spring,spring-json,Java,Json,Spring,Spring Json,@ResponseBody返回 [{“id”:1010,“name”:“projectname2”}]键入json字符串 但是我需要下面的json字符串 [{“id”:1010,“name”:“projectname2”,“age”:“21”}] 那么,如何将属性转换为默认的json字符串呢 Im使用java spring mvc框架和spring json jar @RequestMapping(value = "/projectsByEmployeeId/list", method = Re

@ResponseBody
返回

[{“id”:1010,“name”:“projectname2”}]
键入json字符串

但是我需要下面的json字符串

[{“id”:1010,“name”:“projectname2”,“age”:“21”}]

那么,如何将属性转换为默认的json字符串呢

Im使用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();
    }
}
@RequestMapping(value=“/projectsByEmployeeId/list”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@应答器
公共列表getProjectsByEmployeeId(
HttpServletRequest(请求){
字符串过滤器=request.getParameter(“过滤器”);
FilterAttributes[]FilterAttributes=null;
试一试{
filterAttributes=new ObjectMapper().readValue(过滤器,
过滤器属性[]类别);
}捕获(异常){
logger.error(“过滤参数JSON字符串传递失败”,
例外情况);
}
if((filterAttributes!=null)
&&(!filterAttributes[0].getStringValue().isEmpty()){
return utilityService.getProjectsByEmployeeId(44L);//这是一个示例id
}否则{
返回utilityService.getProjects();
}
}

实现这一点的另一种方法(您说现在正在使用JSONObject,而JSONObject不是默认库)

拿绳子

[{"id":1010,"name":"projectname2"}]
并将其转换为JSONObject


转换后,您可以使用添加值为“21”的键“age”。

另一种方法是纯字符串操作-

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}]

+1包括您正在使用的语言-我会看看是否能很快得到答案编辑:您使用的是Gson还是常规JSONObject?您是如何生成JSON(方法代码)的?你能粘贴它吗?是的,我在Java中使用常规JSONObject,但你说没有使用任何库?我已经更新了我的代码。我需要在返回属性之前添加新属性。我如何添加它?