[英]How to parse and object that have inside an array of numbers using com.fasterxml.jackson.databind.ObjectMapper
I'm working on a Spring MVC project using angularjs, I'm passing from the view the following JSON array {name:"theName", code:"theCode", comment:"theComment", listOfIds:["3", "4"]}
我正在使用 angularjs 处理 Spring MVC 项目,我从视图中传递以下 JSON 数组{name:"theName", code:"theCode", comment:"theComment", listOfIds:["3", "4"]}
This is my controller that receives the JSON这是我接收 JSON 的控制器
@RequestMapping(value = "/createPerson/", method = RequestMethod.POST)
public ResponseEntity<Void> createSep(@RequestBody final String DTOJsonObject,
UriComponentsBuilder ucBuilder) {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> dataDTOParsed ;
dataDTOParsed = mapper.readValue(DTOJsonObject, HashMap.class);
//I tried this but it says that a String can not be converted to ArrayList<String>
ArrayList<String> listIDS = new ArrayList<String>();
listIDS = (ArrayList<String>) dataDTOParsed .get("listOfIds");
}
I tried using the following cast but it says that String can not be converted to ArrayList<String>
我尝试使用以下强制String can not be converted to ArrayList<String>
但它说String can not be converted to ArrayList<String>
ArrayList<String> listIDS = new ArrayList<String>();
listIDS = (ArrayList<String>) dataDTOParsed .get(this);
I know how to get the other values is this value listOfIds:["3", "4"]
that is giving me problems我知道如何获取其他值是这个值listOfIds:["3", "4"]
这给我带来了问题
Since your JSON object has properties with different data types (String, Array...) it would be better to map it to a Map<String, Object>
and then cast accordingly.由于您的 JSON 对象具有不同数据类型(字符串、数组...)的属性,因此最好将其映射到Map<String, Object>
然后进行相应的转换。
Code sample:代码示例:
@RequestMapping(value = "/createPerson/", method = RequestMethod.POST)
public ResponseEntity<Void> createSep(@RequestBody final String DTOJsonObject,
UriComponentsBuilder ucBuilder) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> parsedData;
parsedData = mapper.readValue(DTOJsonObject, HashMap.class);
List<String> listIDS = (ArrayList<String>)parsedData.get("listOfIds");
for(String n : listIDS){
System.out.println(n);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.