Java Json中的控件字符导致JsonParsingException

Java Json中的控件字符导致JsonParsingException,java,json,exception,jsonreader,Java,Json,Exception,Jsonreader,我有一个java方法: public class ReadIssues { // Reads from a Json object protected JsonArray readJson() throws IOException { JsonArray issueArray = null; ClassLoader cl = Thread.currentThread().getContextClassLoader(); // Get input strea

我有一个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()。我认为JSON中的
body
值包含
\n
\r
等字符

我看到一篇帖子建议做一些类似于
json=json.replaceAll(“\r?\n”,”)的事情
但这就是
json
字符串的地方。在我使用JsonReader对象的情况下应该怎么做

JSON示例

[ { url: 'https://api.github.com/repos/TestOrg/test2/issues/4',
repository_url: 'https://api.github.com/repos/TestOrg/test2',
labels_url: 'https://api.github.com/repos/TestOrg/test2/issues/4/labels{/name}',
comments_url: 'https://api.github.com/repos/TestOrg/test2/issues/4/comments',
events_url: 'https://api.github.com/repos/TestOrg/test2/issues/4/events',
html_url: 'https://github.com/TestOrg/test2/issues/4',
id: 347593311,
node_id: 'MDU6SXNzdWUzNDc1OTMzMTE=',
number: 4,
title: 'test issue 2',
user:
 { login: 'my-repo',
   id: 32067576,
   node_id: 'MDQ6VXNlcjMyMDY3NTc2',
   avatar_url: 'https://avatars1.githubusercontent.com/u/32067576?v=4',
   gravatar_id: '',
   url: 'https://api.github.com/users/my-repo',
   html_url: 'https://github.com/my-repo',
   followers_url: 'https://api.github.com/users/my-repo/followers',
   following_url: 'https://api.github.com/users/my-repo/following{/other_user}',
   gists_url: 'https://api.github.com/users/my-repo/gists{/gist_id}',
   starred_url: 'https://api.github.com/users/my-repo/starred{/owner}{/repo}',
   subscriptions_url: 'https://api.github.com/users/my-repo/subscriptions',
   organizations_url: 'https://api.github.com/users/my-repo/orgs',
   repos_url: 'https://api.github.com/users/my-repo/repos',
   events_url: 'https://api.github.com/users/my-repo/events{/privacy}',
   received_events_url: 'https://api.github.com/users/my-repo/received_events',
   type: 'User',
   site_admin: false },
    labels: [],
    state: 'open',
    locked: false,
    assignee: null,
    assignees: [],
    milestone: null,
    comments: 0,
    created_at: '2018-08-04T06:34:50Z',
    updated_at: '2018-08-04T06:34:50Z',
    closed_at: null,
    author_association: 'CONTRIBUTOR',
    body: 'In order to help resolve your issue as quickly as possible please provice the following information when \r\n\r\n1. What is the issue?\r\nAnother one!\r\n\r\n2. What are the steps to recreate the issue?\r\n\r\n\r\n3. Do you have any relevant code samples? If so, please provice them.\r\n\r\n\r\n4. Have you made any specific configurations?\r\n\r\n\r\n5. Do you get an error? If so please include it and any stack trace.\r\n\r\n\r\n6. What version of the API are you using?\r\n',
performed_via_github_app: null,
score: 1 },
  { url: 'https://api.github.com/repos/TestOrg/test2/issues/2',
    repository_url: 'https://api.github.com/repos/TestOrg/test2',
    labels_url: 'https://api.github.com/repos/TestOrg/test2/issues/2/labels{/name}',
    comments_url: 'https://api.github.com/repos/TestOrg/test2/issues/2/comments',
    events_url: 'https://api.github.com/repos/TestOrg/test2/issues/2/events',
    html_url: 'https://github.com/TestOrg/test2/issues/2',
    id: 324450775,
    node_id: 'MDU6SXNzdWUzMjQ0NTA3NzU=',
    number: 2,
    title: 'Something really bad',
    user:
     { login: 'my-repo',
       id: 32067576,
       node_id: 'MDQ6VXNlcjMyMDY3NTc2',
       avatar_url: 'https://avatars1.githubusercontent.com/u/32067576?v=4',
       gravatar_id: '',
       url: 'https://api.github.com/users/my-repo',
       html_url: 'https://github.com/my-repo',
       followers_url: 'https://api.github.com/users/my-repo/followers',
       following_url: 'https://api.github.com/users/my-repo/following{/other_user}',
       gists_url: 'https://api.github.com/users/my-repo/gists{/gist_id}',
       starred_url: 'https://api.github.com/users/my-repo/starred{/owner}{/repo}',
       subscriptions_url: 'https://api.github.com/users/my-repo/subscriptions',
       organizations_url: 'https://api.github.com/users/my-repo/orgs',
       repos_url: 'https://api.github.com/users/my-repo/repos',
       events_url: 'https://api.github.com/users/my-repo/events{/privacy}',
       received_events_url: 'https://api.github.com/users/my-repo/received_events',
       type: 'User',
       site_admin: false },
    labels: [],
    state: 'open',
    locked: false,
    assignee: null,
    assignees: [],
    milestone: null,
    comments: 1,
    created_at: '2018-05-18T15:15:21Z',
    updated_at: '2018-06-22T12:45:17Z',
    closed_at: null,
    author_association: 'CONTRIBUTOR',
    body: 'In order to help resolve your issue as quickly as possible please provice the following information when \r\n\r\n1. What is the issue?\r\nIts all going wrong!!\r\n\r\n2. What are the steps to recreate the issue?\r\nStep 1\r\nStep 2\r\nStep n\r\n\r\n\r\n3. Do you have any relevant code samples? If so, please provice them.\r\nbla\r\n\r\n4. Have you made any specific configurations?\r\nN/A\r\n\r\n5. Do you get an error? If so please include it and any stack trace.\r\nError: Invalid input\r\n\r\n6. What version of the API are you using?\r\n1.2.0\r\n',
performed_via_github_app: null,
score: 1 } ]

您的输入不是有效的JSON。第一行是最好的例子

[ { url: 'https://api.github.com/repos/TestOrg/test2/issues/4',
JSON中的所有对象键名称都需要加引号。因此,第一行已更正:

[ { "url": 'https://api.github.com/repos/TestOrg/test2/issues/4',
对象中的所有其他键名称也是如此。您的输入实际上是有效的JavaScript。为了更正您的字符串,我刚刚将您的原始文本粘贴到javascript控制台(节点或浏览器F12控制台将起作用)并执行了以下操作:

x = <paste of your string above>
JSON.stringify(x)

您的输入不是有效的JSON。第一行是最好的例子

[ { url: 'https://api.github.com/repos/TestOrg/test2/issues/4',
JSON中的所有对象键名称都需要加引号。因此,第一行已更正:

[ { "url": 'https://api.github.com/repos/TestOrg/test2/issues/4',
对象中的所有其他键名称也是如此。您的输入实际上是有效的JavaScript。为了更正您的字符串,我刚刚将您的原始文本粘贴到javascript控制台(节点或浏览器F12控制台将起作用)并执行了以下操作:

x = <paste of your string above>
JSON.stringify(x)

向我们展示导致抛出异常的示例JSON,
GitHubIssue.JSON
。如果json的非空白字符不是
[
,readArray可能会抛出。添加了JSON示例。向我们展示导致抛出异常的JSON示例,
GitHubIssue.JSON
。这可能会揭示真正的问题。如果JSON的非空白字符不是
[
,预计readArray会抛出。添加了JSON示例。啊,我整晚都盯着它看,却没有注意到。谢谢你的帮助和关于漂亮打印的好提示。啊,我整晚都盯着它看,却没有注意到。谢谢你关于漂亮打印的帮助和好提示。