-3

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.

JieBaef
  • 122
  • 1
  • 11
  • 1
    To find a solution: Step 1) **Learn JSON**. I mean, come on, the syntax is so simple it can be described on a single page: http://json.org/. --- Then: Step 2) Realize that `[` is the beginning of a JSON **Array**. --- Step 3) Read the [documentation](https://stleary.github.io/JSON-java/) of the JSON library you're using. When you do, you'd realize that class [**`JSONArray`**](https://stleary.github.io/JSON-java/org/json/JSONArray.html) may be more appropriate than [`JSONObject`](https://stleary.github.io/JSON-java/org/json/JSONObject.html) to parse a JSON Array. – Andreas Aug 03 '18 at 02:21
  • I just searched for a fast solution and when i googled, the first stackoverflow link which popped up was the one I am using at the moment. I guess it won't be that easy and will try what you advised me to do. I've never used JSON so I have no idea what it is besides that it's something like a multidimensional list – JieBaef Aug 03 '18 at 07:34
  • Read the first sentence in the [link](http://json.org/) I gave: *"JSON (JavaScript Object Notation) is a lightweight **data-interchange format**".* It is not a "multidimensional list". – Andreas Aug 03 '18 at 16:06

2 Answers2

1

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.

MetroidFan2002
  • 29,217
  • 16
  • 62
  • 80
0

I'm not sure if your problem was solved. When I started using JSON for the first time I encountered the same error. Posting my answer here so if anyone else ends up here searching for an answer can find it easily. This error comes when you are not passing the right parameters to the right get function call. We actually have to go step by step in. First, we need to access the root object and then we have to get the desired JSON Array. Once we have the JSONArray we can call a get(i) and further on that we can call getString(param) or getInt(param).

I have created sample projects. The first one is for JSON object creation and the second one is for JSON parsing.

Creation of JSON Object: https://github.com/vikram-bhardwaj/RestServer_Mar2022 Parsing the JSON File: https://github.com/vikram-bhardwaj/RestClient_Mar2022

Hope it helped.