获取对象内的所有对象 JSON Java Android
Getting all objects within an object JSON Java Android
所以我正在尝试消耗英雄联盟 API,其中 returns 一个 JSON 响应。我没有使用像 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"}}}
但是当我尝试访问数据对象中的对象时,我必须在我的代码中明确列出键名。
而且键名是动态的,不实用。
有没有办法在不显式调用键名的情况下获取数据对象中的所有对象?
这是我的 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);
}
}
使用 keys()
获取 JSON 对象的键,然后迭代它们。
Iterator<String> keys = json.keys();
while (keys.hasNext())
{
// Get the key
String key = keys.next();
// Get the value
JSONObject value = json.getJSONObject(key);
// Do something...
}
您可以使用 Iterator 来解析数据
Iterator<?> keys = jObject.keys();
while( keys.hasNext() ) {
String key = (String)keys.next();
if ( jObject.get(key) instanceof JSONObject ) {
...
...
..
}
}
这是一个从 JSON 文档中递归获取所有值的示例。
这里使用了import org.json.*
public static void printJSON(JSONObject jsonObj) {
//Iterating Key Set
for (Object keyObj : jsonObj.keySet()) {
String key = (String) keyObj;
Object valObj = jsonObj.get(key);
//If next entry is Object
if (valObj instanceof JSONObject) {
// call printJSON on nested object
printJSON((JSONObject) valObj);
}
//iff next entry is nested Array
else if (valObj instanceof JSONArray) {
System.out.println("NestedArraykey : " + key);
printJSONArray((JSONArray) valObj);
} else {
System.out.println("key : " + key);
System.out.println("value : " + valObj.toString());
}
}
}
public static void printJSONArray(JSONArray jarray) {
//Get Object from Array
for (int i = 0; i < jarray.length(); i++) {
printJSON(jarray.getJSONObject(i));
}
}
所以我正在尝试消耗英雄联盟 API,其中 returns 一个 JSON 响应。我没有使用像 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"}}}
但是当我尝试访问数据对象中的对象时,我必须在我的代码中明确列出键名。
而且键名是动态的,不实用。
有没有办法在不显式调用键名的情况下获取数据对象中的所有对象?
这是我的 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);
}
}
使用 keys()
获取 JSON 对象的键,然后迭代它们。
Iterator<String> keys = json.keys();
while (keys.hasNext())
{
// Get the key
String key = keys.next();
// Get the value
JSONObject value = json.getJSONObject(key);
// Do something...
}
您可以使用 Iterator 来解析数据
Iterator<?> keys = jObject.keys();
while( keys.hasNext() ) {
String key = (String)keys.next();
if ( jObject.get(key) instanceof JSONObject ) {
...
...
..
}
}
这是一个从 JSON 文档中递归获取所有值的示例。 这里使用了import org.json.*
public static void printJSON(JSONObject jsonObj) {
//Iterating Key Set
for (Object keyObj : jsonObj.keySet()) {
String key = (String) keyObj;
Object valObj = jsonObj.get(key);
//If next entry is Object
if (valObj instanceof JSONObject) {
// call printJSON on nested object
printJSON((JSONObject) valObj);
}
//iff next entry is nested Array
else if (valObj instanceof JSONArray) {
System.out.println("NestedArraykey : " + key);
printJSONArray((JSONArray) valObj);
} else {
System.out.println("key : " + key);
System.out.println("value : " + valObj.toString());
}
}
}
public static void printJSONArray(JSONArray jarray) {
//Get Object from Array
for (int i = 0; i < jarray.length(); i++) {
printJSON(jarray.getJSONObject(i));
}
}