Unexpected character ('P' (code 80)): was expecting comma to separate OBJECT entries

【问题】

从文件中读字符串内容,使用  org.codehaus.jackson.map.ObjectMapper。将json转java对象时,

异常:org.codehaus.jackson.JsonParseException: Unexpected character ('P' (code 80)): was expecting comma to separate OBJECT entries


【原因】

异常:以逗号分隔对象...

检查源文件的json字符,json格式正确。

而是在读文件时,读出来的json字符串乱码,导致json格式错误

【解决】按照与文件相符的编码读取文件内容


    public static String readFile(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            String s = null;
            while((s = br.readLine())!=null){
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString().replace("\r\n", "");
    }


你可能感兴趣的:(Unexpected character ('P' (code 80)): was expecting comma to separate OBJECT entries)