我想通过 API 检索足球运动员列表,我已正确进行 Http 处理程序和 api 调用。现在我有了这个 JSON 数组
http://api.football-data.org/v1/teams/66/players
我想对其进行解析,以便仅显示玩家的姓名。我如何解析 JSON 数组的第一位,以便数组从 [{name:Paul Pogba... 开始?
到目前为止我的代码:
@Override
protected Void doInBackground(Void... arg0) {
//New instance of http
http sh = new http();
// Making a request to URL and getting response
final String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from: " + jsonStr);
if (jsonStr != null) {
try {
// Getting JSON Array node
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jo = jsonarray.getJSONObject(i);
String name = jo.getString("name");
HashMap<String, String> player = new HashMap<>();
player.put("name", name);
playerlist.add(player);
}
} catch (final JSONException e) { //In case an error regarding JSON parsing takes place
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server."); //In case the JSON can't be obtained from the server
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get JSON from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
最佳答案
谢谢各位提供的信息。用子字符串修复了它:)
关于java - 解析 JSON 数组和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745568/