Java 解析JSON数组和对象
我想通过API检索足球运动员的名单,我已经正确地进行了Http处理程序和API调用。现在我有了这个JSON数组 我想解析它,以便只显示玩家的名字。如何解析JSON数组的第一位,使数组从[{name:Paul Pogba…please]开始 到目前为止,我的代码是:Java 解析JSON数组和对象,java,android,Java,Android,我想通过API检索足球运动员的名单,我已经正确地进行了Http处理程序和API调用。现在我有了这个JSON数组 我想解析它,以便只显示玩家的名字。如何解析JSON数组的第一位,使数组从[{name:Paul Pogba…please]开始 到目前为止,我的代码是: @Override protected Void doInBackground(Void... arg0) { //New instance of http http sh = new ht
@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;
}
@覆盖
受保护的Void doInBackground(Void…arg0){
//http的新实例
http sh=新的http();
//向URL发出请求并获得响应
最终字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自:+jsonStr的响应”);
if(jsonStr!=null){
试一试{
//获取JSON数组节点
JSONArray JSONArray=新JSONArray(jsonStr);
for(int i=0;i
感谢信息专家。用子字符串修复了它:)感谢信息专家。用子字符串修复了它:)发布您尝试的代码以及问题。尝试使用像gson或jackson这样的json解析器库。发布您尝试的代码以及问题。尝试使用像gson或json这样的json解析器库杰克逊。