Read JSON error: org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

3k Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

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.