有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javacom。fasterxml。杰克逊。果心JsonParseException:读取json文件时出现意外字符(代码160)

我正在从一个文件中读取下面的json内容并将其转换为一个映射,但我得到了下面的异常。如果有人遇到这样的问题,请告诉我。我验证了我的json内容,看起来有效。不知道为什么会出现这个错误

Json内容:

{
    "Results":[{         
        "TotalPositiveFeedbackCount": 0      
    },{
        "TotalPositiveFeedbackCount": 1      
    }   ]
}

代码:

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>>() {});
} 

例外情况详情:

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]

共 (1) 个答案

  1. # 1 楼答案

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

    enter image description here

    你可以用它来修理

    responseJson = responseJson.replace('\u00A0',' ');