そのため、JSON 応答を返す League of Legends API を使用しようとしています。私は Jakcson や GSON のようなファンシーなライブラリを使用していません。
{"type":"champion","version":"5.11.1","data":{"Thresh":{"id":412,"key":"Thresh","name":"Thresh","title":"the Chain Warden"},"Aatrox":{"id":266,"key":"Aatrox","name":"Aatrox","title":"the Darkin Blade"},"Tryndamere":{"id":23,"key":"Tryndamere","name":"Tryndamere","title":"the Barbarian King"},"Gragas":{"id":79,"key":"Gragas","name":"Gragas","title":"the Rabble Rouser"}}}
しかし、データ オブジェクト内のオブジェクトにアクセスしようとすると、コードにキー名を明示的にリストする必要があります。
また、キー名は非常に動的であるため、実用的ではありません。
キー名を明示的に呼び出さずに、データ オブジェクト内のすべてのオブジェクトを取得する方法はありますか?
Heres私のJavaクライアント側のコード
public String callApi(String API_URL) throws IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(API_URL)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public void buttonClick(View view) throws IOException, JSONException {
String API_URL = "https://global.api.pvp.net/api/lol/static-data/tr/v1.2/champion?locale=en_US&api_key=my_key_here";
String champions_json = callApi(API_URL);
Log.i("summoner",champions_json);
JSONObject json= new JSONObject(champions_json);
JSONObject data = json.getJSONObject("data");
Log.i("summoner", String.valueOf(data));
List<String> champions = new ArrayList<String>();
champions.add("Garen");
champions.add("Aatrox");
champions.add("Thresh");
for (String champion : champions) {
JSONObject object = new JSONObject(String.valueOf(data));
String name = object.getString(champion);
Log.i("summoner",name);
}
}