Json中的控制字符导致JsonParsingException

时间:2018-08-11 19:53:59

标签: 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 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", "");的帖子,但这就是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 } ]

1 个答案:

答案 0 :(得分:3)

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

[ { url: 'https://api.github.com/repos/TestOrg/test2/issues/4',

JSON中的所有对象键名称都必须用引号引起来。因此,第一行已更正:

[ { "url": 'https://api.github.com/repos/TestOrg/test2/issues/4',

Ditto用于对象中的所有其他键名。您输入的内容实际上是有效的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
  }
]