简体   繁体   English

如何使用 com.fasterxml.jackson.databind.ObjectMapper 解析和对象包含在数字数组中

[英]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.

相关问题 使用com.fasterxml.jackson.databind.ObjectMapper序列化接口字段 - serialize interface fields using com.fasterxml.jackson.databind.ObjectMapper 无法解析导入com.fasterxml.jackson.databind.ObjectMapper - The import com.fasterxml.jackson.databind.ObjectMapper cannot be resolved Flink作业:获取InvalidClassException:com.fasterxml.jackson.databind.ObjectMapper - Flink Job : Getting InvalidClassException: com.fasterxml.jackson.databind.ObjectMapper com.fasterxml.jackson.databind.ObjectMapper编码用于本地化字符 - com.fasterxml.jackson.databind.ObjectMapper encoding for localised characters Proguard:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.ObjectMapper - Proguard: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper 为什么不在com.fasterxml.jackson.databind.ObjectMapper static中创建一些方法呢? - Why not make some methods in com.fasterxml.jackson.databind.ObjectMapper static? Spring Boot/@JDBCTest - 没有可用的“com.fasterxml.jackson.databind.ObjectMapper”类型的合格 bean - Spring Boot/@JDBCTest - No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available java.lang.ClassNotFoundException:Java 11中的com.fasterxml.jackson.databind.ObjectMapper - java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper in Java 11 Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper in IntelliJ after upgrading to Java 11 - Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper in IntelliJ after upgrading to Java 11 使用com.fasterxml.jackson.databind.JsonNode从Java中的json数组解析json对象; - parse json object from json array in java using com.fasterxml.jackson.databind.JsonNode;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM