Jackson JSON解析器无效utf-8启动字节

时间:2012-12-11 23:37:10

标签: java json exception jackson

我正在尝试解析以下JSON并且我不断收到JsonParseException:

{
   "episodes":{
      "description":"Episode 3 – Oprah's Surprise Patrol from 1\/20\/04\nTake a trip down memory lane and hear all your favorite episodes of The Oprah Winfrey Show from the last 25 seasons -- everyday on your radio!"
   }
}

在这个JSON

中也失败了
{
   "episodes":{
      "description":"After 20 years in sports talk…he’s still the top dog!  Catch Christopher “Mad Dog” Russo weekday afternoons on Mad Dog Radio as he tells it like it is…Give the Doggie a call at 888-623-3646."
   }
}

例外:

org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0x96
 at [Source: C:\Json Test Files\episodes.txt; line: 3, column: 33]
    at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
    at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
    at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidInitial(Utf8StreamParser.java:2236)
    at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidChar(Utf8StreamParser.java:2230)
    at org.codehaus.jackson.impl.Utf8StreamParser._finishString2(Utf8StreamParser.java:1467)
    at org.codehaus.jackson.impl.Utf8StreamParser._finishString(Utf8StreamParser.java:1394)
    at org.codehaus.jackson.impl.Utf8StreamParser.getText(Utf8StreamParser.java:113)
    at com.niveus.jackson.Main.parseEpisodes(Main.java:37)
    at com.niveus.jackson.Main.main(Main.java:13)

代码:

public class Main {
    public static void main(String [] args) {
        parseEpisodes("C:\\Json Test Files\\episodes.txt");
    }
    public static void parseEpisodes(String filename) {
        JsonFactory factory = new JsonFactory();
        JsonParser parser = null;
        String nameField = null;

        try {
            parser = factory.createJsonParser(new File(filename));

            parser.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
            parser.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);

            JsonToken token = parser.nextToken();
            nameField = parser.getText();
            String desc = null;

            while (token != JsonToken.END_OBJECT) {
                if (nameField.equals("episodes")) {
                    while (token != JsonToken.END_OBJECT) {
                        if (nameField.equals("description")) {
                            parser.nextToken();
                            desc = parser.getText();
                        }

                        token = parser.nextToken();
                        nameField = parser.getText();
                    }
                }

                token = parser.nextToken();
                nameField = parser.getText();
            }

            System.out.println(desc);
        }
        catch (JsonParseException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:17)

第33列的字符为,这就是字节0x96的原因是该文件在物理上编码为Windows-1252。您需要以UTF-8保存文件,windows-1252不是json的有效编码。如何执行此操作取决于您使用的文本编辑器。

请参阅JSON RFC

  
      
  1. 编码

         

    JSON文本应以Unicode编码。默认编码为
      UTF-8。

  2.   

答案 1 :(得分:1)

我也遇到过类似的问题。在记事本++中打开json,然后在编码下拉列表中选择UTF-8。并将文本保存到其他文件。这样做可以解决问题。