我有这个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 对象的情况下,我应该怎么做?
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 } ]