解析包含复杂数据的Json对象

时间:2013-01-10 06:43:24

标签: json spring java-ee jackson

无法解析包含两个简单字段和一个对象的json字符串。我使用杰克逊库。我总是得到400错误

JSON:

{
    "id": "",
    "categories": "{"name":"Sport","id":1}",
    "name": "sdsd"
}

控制器:

@RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> createCategory(HttpServletRequest request, @RequestBody Polls polls) {


        pollsService.add(polls);

        URI uri = new UriTemplate("{requestUrl}/{username}").expand(request.getRequestURL().toString(), polls.getId());
        final HttpHeaders headers = new HttpHeaders();
        headers.put("Location", Collections.singletonList(uri.toASCIIString()));
        return new ResponseEntity<String>(headers, HttpStatus.CREATED);
    }

1 个答案:

答案 0 :(得分:0)

  

包含两个简单字段和一个对象。

这不是您在示例代码中发布的内容。可能意味着代表JSON的示例代码实际上不是有效的JSON。它包含两个字符串元素,而不是JSON。

以下是一个有效的JSON示例,类似于最初发布的内容。

{
    "id": "",
    "categories": {"name":"Sport","id":1},
    "name": "sdsd"
}
相关问题