json無法解析Unrecognized token:was expecting (‘true’, ‘false’ or ‘null’)

fasterxml: Unrecognized token ‘open’: was expecting (‘true’, ‘false’ or ‘null’)

  • 代碼片段
    public class JsonUtil {
        
        private static final ObjectMapper MAPPER = new ObjectMapper();
    
        public static void main(String[] args) throws Exception {
    
        String s = "testjson";
    
        String s1 = MAPPER.readValue(s, String.class);
    
        System.out.println(s1);
    }
    
    }

     

  • exception
    Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'testjson': was expecting ('true', 'false' or 'null')
     at [Source: java.io.StringReader@117c323d; line: 1, column: 9]

     

  • 錯誤原因 
    fasterxml無法對這個字符串進行判斷,它的處理邏輯應該是先判斷這個字符串是否是boolean,因爲這個字符串並不是用雙引號引起來的字符串。
  • 解決辦法 
    String s = “testjson”;應改爲String s = "\"testjson\"";,此時纔可被fasterxml正確理解。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章