-3

正如您在标题中看到的那样,当我尝试读取 Riot-Games API 中可用的 JSON 文件时出现错误。我尝试使用其 SummonerID 返回用户的当前等级和等级。当我尝试获取 SummonerID 时,我没有收到此错误。我猜问题是 JSON 文件以“ [ ”和“ ] ”开头和结尾。因此,我正在寻找如何提取其中某些部分的解决方案(例如:等级、排名和联赛积分)。

这就是我收到召唤者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

杰巴夫 | 芬恩

使用的代码来自stackoverflow,而不是我,但似乎没有像预期的那样完美。

4

1 回答 1

0

您试图阅读的是“json 数组”中包含的两个“json 对象”。我不熟悉您正在使用的库(我更喜欢 Jackson),但应该有一种方法可以将此字符串读取为 json array然后从中检索两个 json 对象。

于 2018-08-03T02:23:06.380 回答