Issue
As you can see in the title, I get an error when I try to read a JSON file which is available at the Riot-Games API. I try to return the current tier and rank of a user using its summonerID. I don't get this error when I try to obtain the summonerID. I guess the problem is that the JSON file starts and ends with "[" and "]". Therefore I'm searching a solution on how to extract some parts of it (e.g.: tier, rank and leaguePoints).
This is how I recieve the summonerID:
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();
}
This is how I try to recieve the informations about the current tier:
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();
}
The JSON file to obtain looks like this:
[
{
"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
}
]
And the exact error code is:
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)
Almost forgot about the methods readJsonFromURL and 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();
}
Thanks for all your assistance :D
JieBäf | Finn
The used code is from stackoverflow and not by me but seems not to work as perfect as intended.
Solution
What you are attempting to read is two "json objects" contained within a "json array". I'm not familiar with the library you're using (I prefer Jackson) but there should be a way to read this string as a json array, then retrieve the two json objects from it.
Answered By - MetroidFan2002
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.