com.fasterxml.jackson.core.JsonParseException:读取 json 文件时出现意外字符(代码 160)

use*_*926 4 java json jakarta-ee

我正在从文件中读取以下 json 内容并转换为地图,但出现以下异常。请让我知道是否有人遇到过这样的问题。我验证了我的 json 内容并且看起来有效。不知道为什么这个错误。

JSON 内容:

{
    "Results":[{         
        "TotalPositiveFeedbackCount": 0      
    },{
        "TotalPositiveFeedbackCount": 1      
    }   ]
}
Run Code Online (Sandbox Code Playgroud)

代码:

Map<String, Object> domainMap = new HashMap<String, Object>();
try {
    responseJson = getFile("reviewresponse.json");
    //responseJson = new String(Files.readAllBytes(Paths.get("reviewresponse.json")), StandardCharsets.UTF_8);
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    domainMap = jsonObjectMapper.readValue(responseJson,
                   new TypeReference<Map<String, Object>>() {});
} 
Run Code Online (Sandbox Code Playgroud)

异常详情:

com.fasterxml.jackson.core.JsonParseException: Unexpected character (' ' (code 160)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name
 at [Source: {
    "Results":[{         
        "TotalPositiveFeedbackCount": 0      
    },{
        "TotalPositiveFeedbackCount": 1      
    }   ]
}
; line: 2, column: 15]
Run Code Online (Sandbox Code Playgroud)

dus*_*ltz 5

您的 JSON 内容包含不间断空格(字符代码 160,通常称为&nbsp;),可能来自复制和粘贴用于&nbsp;缩进 JSON 的JSON(通常来自网页)。

在此处输入图片说明

你可以用

responseJson = responseJson.replace('\u00A0',' ');
Run Code Online (Sandbox Code Playgroud)