java 尝试将JSON字符串响应从API转换为Map< String,Object>时出错

n3h0vuf2  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(162)

我在使用返回JSON字符串的API时遇到了一个问题。当尝试将JSON字符串响应从API转换为Map <String,Object>时,它会抛出错误。但是,当我手动将相同的JSON字符串响应嵌入到代码中并执行到Map <String,Object>的转换时,它可以无缝工作。事实证明,理解此行为对我来说相当具有挑战性
错误:
在上下文初始化过程中遇到异常-取消刷新尝试:org. springframework. beans. factory. BeanCreationException:创建名为'testService'的bean时出错:调用init方法失败;嵌套异常为java. lang。RuntimeException:com. fasterxml. jackson. databind. exc. MismatchedInputException:无法构造java.util.LinkedHashMap的示例(尽管至少存在一个创建者):没有要从[Source:(String)""{"DXX ":{" NX":"XXDHX XXK XIMIXED","X00165401100000087","NXme1":"XINHX","XXhmId":"16401","XXhmNXme":"16401"]处的String值("{")中进行格式化的String参数构造函数/工厂方法:"MXXXEXXXXD WXXXD","Incubation":"1","XXXNXme":"XXXIVE","XXXXhgDX":"0000000","XpeXXXXMXinX":"XYX "," DXXXXMXinX ":" 0000000 "," XvXXXg ":" I "," XvXXXgNXme ":" INDIVIDUXX "," XuXXDiXXXXg ":" N","XuXXDiXXXXXDX":"0000000","XuXXDiXXDX":"0000000000000000000000 "," XXkMXde ":" P "," DeXqMXde ":" P "," HighXedeemXmX ":" 0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXXnd":"200"," XXdPXXXXXughX":" 0"," XXdPXXXnuX":"" 0","[截断的1399个字符];行:1,列:1]
JSON字符串:

{
  "DXX": {
    "NX": "XXDHX XXK XIMIXED",
    "XXXX": "X001654011000000087",
    "NXme1": "XINHX",
    "XXhmId": "16401",
    "XXhmNXme": "MXXXEXXXXD WXXXD",
    "InXXXX": "1",
    "XXXNXme": "XXXIVE",
    "XXXXhgDX": "00000000",
    "XpeXXXXMXinX": "XYX",
    "DXXXXMXinX": "00000000",
    "XvXXXg": "I",
    "XvXXXgNXme": "INDIVIDUXX",
    "XuXXDiXXXXg": "N",
    "XuXXDiXXXXXDX": "00000000",
    "XuXXDiXXXXXX": "0000000000000000000000000",
    "XXkMXde": "P",
    "DeXqMXde": "P",
    "HighXedeemXmX": "0",
    "HighXedeemPXX": "0",
    "XXXXXhVXX": "0",
    "XXdPXXEXXnd": "70610",
    "XXdPXXXXughX": "0",
    "XXdPXXXnuX": "0",
    "XXdPXXXedeemed": "967",
    "XXdPXXXXXed": "0",
    "XXdPXXExpiXed": "0",
    "XXdPXXXdjIn": "10000",
    "XXdPXXXdjXuX": "0",
    "GXXXeXXd": "0",
    "XyXXXDXXXX": "10198",
    "XXdXpndEXXnd": "0",
    "YXdXpendEXXnd": "0",
    "XiXeXXDXXXX": "79643",
    "PXPXXXNXXXXd": "0",
    "XpenXXXedXnXXX": "79643",
    "XXmXMXhXXeq": "1",
    "XXmXDXyXXMXh": "15",
    "XXXXXmXDX": "20231215",
    "NexXXXmXDX": "20240115",
    "DXXpened": "20231204",
    "DiXXPkXPXX": "0",
    "DiXXPkXMXnVXXPXX": "0",
    "XuXXDiXXXXeq": "0",
    "XuXXDiXXDXyXXMXh": "0",
    "XXXXDiXXuXXeDX": "00000000",
    "XuXXDiXXXhXeXhXd": "0",
    "XuXXDiXXMXnVXX": "0",
    "WXiveMemXXee": "N",
    "XXXpnDX": "2023338",
    "XeXXXeXmX": "0",
    "XuXXNXd": "2",
    "PeXIXemNXd": "0",
    "PXXXXXeNXd": "7",
    "XnXXyXd": "IND",
    "XuXXXd": "356",
    "NXXMXhDXXXXXuX": "1",
    "MXnXhXyXXXhXXXkGXXup": [
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "20231215"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      }
    ],
    "XXXXXpenXniDXXe": "20241204",
    "XXdDiXXXXXhXXXk": "0",
    "XXdXpendXmX": "0"
  }
}

字符串
验证码:

HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.ALL));
        headers.add("api_key","qwerqwersadfaf");
        headers.add("x-session-id","asdfasdf");
        headers.add("x-user-id","asdfasdfas");
        headers.add("x-message-id","asdfasdfas");

        String bodyTopost = "{\"Data\": {\"Account\": \"SAMPLEaccount\",\"Schm\": \"168401\"}}";

        HttpEntity<String> entity = new HttpEntity<String>(bodyTopost, headers);
        ParameterizedTypeReference<String> parameterizedTypeReference = new ParameterizedTypeReference<String>() {
        };
        String inputUrl = "https://api.testbank.co.in/cons/abcd/v1";

        try {
            ResponseEntity<String> result = restTemplate.exchange(inputUrl, HttpMethod.POST, entity, parameterizedTypeReference);
            if (result.getStatusCode().is2xxSuccessful()) {
                if (result.getBody() != null) {

                     String resultBody = result.getBody().replace("\\", "");

                    logger.info("Response value is {}", resultBody);

                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map =  mapper.readValue(resultBody, Map.class);

                    return map;
                } else {
                    logger.info("Payload is null");
                    return null;
                }
            } else {
                logger.info("Failed to execute api");
                return null;
            }
        } catch (ResourceAccessException e) {
            logger.info("Resource Erro  {} ", e);
            return null;
        } catch (RestClientException e) {
            logger.info("Rest Error Erro  {} ", e);
            return null;

        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }


当尝试将JSON字符串响应从API转换为Map <String,Object>时,它会抛出错误。然而,当我手动将相同的JSON字符串响应嵌入到代码中并执行到Map <String,Object>的转换时,它可以无缝地工作。
String data="{"DXX":{"NX":"XXDHX XXK XIMIXED","XXXX":"X001654011000000087","NXme1":"XINHX","XXhmId":"16401","XXhmNXme":"MXXXEXXXXD WXXXD","InXXXX":"1","XXXNXme":"XXXIVE","XXXXhgDX":"00000000","XpeXXXXMXinX":"XYX","DXXXXMXinX":"00000000","XvXXXg":"I","XvXXXgNXme":"INDIVIDUXX","XuXXDiXXXXg":"N","XuXXDiXXXXXDX":"00000000","XuXXDiXXXXXX":"0000000000000000000000000","XXkMXde":"P","DeXqMXde":"P","HighXedeemXmX":"0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXnd":"200","XXdPXXXXughX":"0","XXdPXXXnuX":"0","XXdPXXXedeemed":"2","XXdPXXXXXed":"0","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","GXXXeXXd":"0","XyXXXDXXXX":"10198","XXdXpndEXXnd":"0","XXdPXXEXXnd":"70610","XXdPXXXXughX":"0","XXdPXXXnuX":"0","XXdPXXXedeemed":"967","XXdPXXXXXed":"0","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","YXdXpendEXXnd":"0","GXXXeXXd":"0","XiXeXXDXXXX":"79643","PXPXXXNXXXXd":"0","XpenXXXedXnXXX":"79643","XXmXMXhXXeq":"1","XXmXDXyXXMXh":"15","XXXXXmXDX":"20231215","NexXXXmXDX":"20240115","DXXpened":"20231204","DiXXPkXPXX":"0","DiXXPkXMXnVXXPXX":"0","XuXXDiXXXXeq":"0","XuXXDiXXDXyXXMXh":"0","XXXXDiXXuXXeDX":"00000000","XuXXDiXXXhXeXhXd":"0","XuXXDiXXMXnVXX":"0","WXiveMemXXee":"N","XXXpnDX":"2023338","XeXXXeXmX":"0","XuXXNXd":"2","PeXIXemNXd":"0","PXXXXXeNXd":"7","XnXXyXd":"IND","XuXXXd":"356","NXXMXhDXXXXXuX":"1","MXnXhXyXXXhXXXkGXXup":[{"DiXXXmX":"0","DiXXMXhDX":"20231215"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"}],"XXXXXpenXniDXXe":"20241204","XXdDiXXXXXhXXXk":"0","XXdXpendXmX":"0"}}";
ObjectMapper mapper = new ObjectMapper(); Map <String,Object> map = mapper. readValue(data,Map.class);
当我直接将响应从API转换为Map <String,Object>时,会发生错误

cld4siwp

cld4siwp1#

从API返回的字符串似乎有两个额外的引号,一个是开始,另一个在字符串的结尾,在堆栈跟踪中查看源属性:[Source: (String)""{"DXX":{"NX":"XXDHX XXK ...
解决这个问题的一个方法是在读取值之前删除额外的引号,就像这样:

int begin = resultBody.indexOf("{");
int end = resultBody.lastIndexOf("}") + 1;
resultBody= resultBody.substring(begin, end);

字符串
用于重现此错误的代码:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class Main {

  public static void main(String[] args) throws JsonProcessingException {

    String resultBody="\"{\"DXX\":{\"NX\":\"XXDHX XXK XIMIXED\",\"XXXX\":\"X001654011000000087\",\"NXme1\":\"XINHX\",\"XXhmId\":\"16401\",\"XXhmNXme\":\"MXXXEXXXXD WXXXD\",\"InXXXX\":\"1\",\"XXXNXme\":\"XXXIVE\",\"XXXXhgDX\":\"00000000\",\"XpeXXXXMXinX\":\"XYX\",\"DXXXXMXinX\":\"00000000\",\"XvXXXg\":\"I\",\"XvXXXgNXme\":\"INDIVIDUXX\",\"XuXXDiXXXXg\":\"N\",\"XuXXDiXXXXXDX\":\"00000000\",\"XuXXDiXXXXXX\":\"0000000000000000000000000\",\"XXkMXde\":\"P\",\"DeXqMXde\":\"P\",\"HighXedeemXmX\":\"0\",\"HighXedeemPXX\":\"0\",\"XXXXXhVXX\":\"0\",\"XXdPXXEXXnd\":\"200\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"2\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"GXXXeXXd\":\"0\",\"XyXXXDXXXX\":\"10198\",\"XXdXpndEXXnd\":\"0\",\"XXdPXXEXXnd\":\"70610\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"967\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"YXdXpendEXXnd\":\"0\",\"GXXXeXXd\":\"0\",\"XiXeXXDXXXX\":\"79643\",\"PXPXXXNXXXXd\":\"0\",\"XpenXXXedXnXXX\":\"79643\",\"XXmXMXhXXeq\":\"1\",\"XXmXDXyXXMXh\":\"15\",\"XXXXXmXDX\":\"20231215\",\"NexXXXmXDX\":\"20240115\",\"DXXpened\":\"20231204\",\"DiXXPkXPXX\":\"0\",\"DiXXPkXMXnVXXPXX\":\"0\",\"XuXXDiXXXXeq\":\"0\",\"XuXXDiXXDXyXXMXh\":\"0\",\"XXXXDiXXuXXeDX\":\"00000000\",\"XuXXDiXXXhXeXhXd\":\"0\",\"XuXXDiXXMXnVXX\":\"0\",\"WXiveMemXXee\":\"N\",\"XXXpnDX\":\"2023338\",\"XeXXXeXmX\":\"0\",\"XuXXNXd\":\"2\",\"PeXIXemNXd\":\"0\",\"PXXXXXeNXd\":\"7\",\"XnXXyXd\":\"IND\",\"XuXXXd\":\"356\",\"NXXMXhDXXXXXuX\":\"1\",\"MXnXhXyXXXhXXXkGXXup\":[{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"20231215\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"}],\"XXXXXpenXniDXXe\":\"20241204\",\"XXdDiXXXXXhXXXk\":\"0\",\"XXdXpendXmX\":\"0\"}}\"";
//    int begin = resultBody.indexOf("{");
//    int end = resultBody.lastIndexOf("}") + 1;
//    resultBody= resultBody.substring(begin, end);
    Map<String, Object> map = new ObjectMapper().readValue(resultBody, Map.class);
    System.out.println(map.entrySet());
  }
}

相关问题