正如您在标题中看到的那样,当我尝试读取Riot-Games API上可用的JSON文件时出现错误。我尝试使用其summonerID返回用户的当前层和等级。尝试获取summonerID时没有出现此错误。 我想问题是JSON文件以“ [”和“ ] ”开头和结尾。因此,我正在寻找有关如何提取其中某些部分(例如:等级,等级和LeaguePoints)的解决方案。
这是我接收到召唤者ID的方式:
public static String getSummonerID(String summoner) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + summoner +"?api_key="+ api_key);
return json.get("id").toString();
}
这是我尝试接收有关当前层的信息的方式:
public static String getSummonerTierSoloQ(String summoner) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://euw1.api.riotgames.com/lol/league/v3/positions/by-summoner/" + getSummonerID(summoner) +"?api_key="+ api_key);
return json.toString();
}
要获取的JSON文件如下:
[
{
"leagueId": "",
"leagueName": "Soraka's Mercenaries",
"tier": "SILVER",
"queueType": "RANKED_SOLO_5x5",
"rank": "III",
"playerOrTeamId": "",
"playerOrTeamName": "JieBäf",
"leaguePoints": 58,
"wins": 142,
"losses": 134,
"veteran": true,
"inactive": false,
"freshBlood": false,
"hotStreak": false
},
{
"leagueId": "",
"leagueName": "Sion's Marksmen",
"tier": "SILVER",
"queueType": "RANKED_FLEX_SR",
"rank": "IV",
"playerOrTeamId": "",
"playerOrTeamName": "JieBäf",
"leaguePoints": 23,
"wins": 96,
"losses": 98,
"veteran": true,
"inactive": false,
"freshBlood": false,
"hotStreak": false
}
]
确切的错误代码是:
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.<init>(JSONObject.java:183)
at org.json.JSONObject.<init>(JSONObject.java:309)
at dev.reader.JsonReader.readJsonFromUrl(JsonReader.java:33)
at dev.reader.JsonReader.getSummonerTierSoloQ(JsonReader.java:56)
at dev.reader.JsonReader.output(JsonReader.java:45)
at dev.main.Load.main(Load.java:15)
几乎忘记了方法readJsonFromURL和readAll:
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
感谢您的协助:D
JieBäf|芬兰人
使用的代码来自stackoverflow,而不是我本人,但似乎不能按预期的那样完美地工作。
答案 0 :(得分:0)
您要读取的是“ json数组”中包含的两个“ json对象”。我不熟悉您使用的库(我更喜欢Jackson),但是应该有一种方法可以将此字符串读取为json array , then 来检索这两个字符串json对象。