Json 中的控制字符导致 JsonParsingException

Control characters in Json causing 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();。我认为 JSON 中的 body 值包含 \n\r 等字符。

我遇到一个 post 建议做类似 json = json.replaceAll("\r?\n", ""); 的事情,但那是 jsonString 的地方。在我使用 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)

然后我 运行 通过 https://jsonformatter.org/ 输出(没有前导引号和尾随引号)进行漂亮的打印,以便在下面返回更正后的文本。

事实上,我就是使用在线漂亮打印机才发现解析错误的。

[
  {
    "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
  }
]