猿问

Json 中的控制字符导致 JsonParsingException

我有这个java方法:


public class ReadIssues {


  // Reads from a Json object

  protected JsonArray readJson() throws IOException {


    JsonArray issueArray = null;


    ClassLoader cl = Thread.currentThread().getContextClassLoader();


    // Get input stream for reading the specified resource

    InputStream inputStream = cl.getResourceAsStream("GitHubIssue.json");


    // Create JsonReader to read JSON data from a stream

    Reader reader = new InputStreamReader(inputStream, "UTF-8");

    JsonReader jsonReader = Json.createReader(reader);


    try{      


      // Create an object model in memory

      issueArray = jsonReader.readArray();

      System.out.println("ReadIssues issueArray: " + issueArray);


    }

    finally {

      if (inputStream != null) {

        inputStream.close();        

      }

      if (jsonReader != null) {

        jsonReader.close();

      }

    }


    return issueArray;


  }

它抛出这个异常:


Exception in thread "main" javax.json.stream.JsonParsingException: Unexpected char 117 at (line no=1, column no=5, offset=4)

    at org.glassfish.json.JsonTokenizer.unexpectedChar(JsonTokenizer.java:601)

    at org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:418)

    at org.glassfish.json.JsonParserImpl$ObjectContext.getNextEvent(JsonParserImpl.java:453)

    at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:363)

    at org.glassfish.json.JsonParserImpl.getObject(JsonParserImpl.java:333)

    at org.glassfish.json.JsonParserImpl.getValue(JsonParserImpl.java:182)

    at org.glassfish.json.JsonParserImpl.getArray(JsonParserImpl.java:326)

    at org.glassfish.json.JsonParserImpl.getArray(JsonParserImpl.java:164)

    at org.glassfish.json.JsonReaderImpl.readArray(JsonReaderImpl.java:129)

    at paulcarron.issuetracker.ReadIssues.readJson(ReadIssues.java:42)

    at paulcarron.issuetracker.App.main(App.java:22)

问题似乎是issueArray = jsonReader.readArray();。我认为body我的 JSON 中的值包含\n和等字符\r。


我遇到了一个帖子,建议做类似的事情,json = json.replaceAll("\r?\n", "");但那json是String. 在我使用 JsonReader 对象的情况下,我应该怎么做?


慕勒3428872
浏览 455回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答