简体   繁体   中英

Control characters in Json causing JsonParsingException

I have this java method:

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;

  }

It throws this exception:

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)

The problem seems to be issueArray = jsonReader.readArray(); . I think that body value in my JSON contains characters such as \\n and \\r .

I came across one post that suggested doing something like json = json.replaceAll("\\r?\\n", ""); but that was where json was a String . What should I do in my case where I'm using a JsonReader object?

JSON Sample

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

Your input is not valid JSON. The first line is the best example.

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

All object key names in JSON need to be in quotes. Hence, first line corrected:

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

Ditto for all the other key names in your objects. Your input is actually valid JavaScript. So to correct your string, I just pasted your original text into a javascript console (node or the browser F12 console will work) and did this:

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

Then I ran the output (sans leading and trailing quotes) through https://jsonformatter.org/ to pretty print to give you back the corrected text below.

As a matter of fact, just using the online pretty printer was how I found the parse error to begin with.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM