我是Android新手学习网络服务我试了很多看这种解析但是找不到这种类似的例子
[
{
"result":"Success",
"matches":[
{
"matchID":"1",
"matchUniqueID":"1134032",
"team_one_name":"Zimbabwe",
"team_two_name":"Afghanistan",
"dateTimeGmt":"2018-02-06T15:00:00.000Z",
"match_type":"T20I",
"match_date":"2018-02-06 00:00:00",
"is_done":"N"
},
{
"matchID":"2",
"matchUniqueID":"1134065",
"team_one_name":"Kent",
"team_two_name":"Leeward Islands",
"dateTimeGmt":"2018-02-06T13:00:00.000Z",
"match_type":"ListA",
"match_date":"2018-02-06 00:00:00",
"is_done":"N"
}
]
}
]
这是我尝试过但不幸的是,它没有用
public class Asy extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
try {
JSONArray jsonArray= new JSONArray(Institute_URL);
for (int i=0;i<jsonArray.length();i++){
JSONObject object=jsonArray.getJSONObject(i);
String result=object.getString("result");
JSONArray innerJsonArray=object.getJSONArray("matches");
for (int j=0;j<innerJsonArray.length();j++){
JSONObject innerObject=innerJsonArray.getJSONObject(j);
Constant.Match_Id.add(String.valueOf(innerObject.get("matchID").toString()));
Log.i("jhk",Constant.Match_Id.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
显示String无法转换为Json数组
答案 0 :(得分:1)
试试这个
try {
JSONArray jsonArray= new JSONArray(response);
for (int i=0;i<jsonArray.length();i++){
JSONObject object=jsonArray.getJSONObject(i);
String result=object.getString("result");
JSONArray innerJsonArray=object.getJSONArray("matches");
for (int j=0;j<innerJsonArray.length();j++){
JSONObject innerObject=innerJsonArray.getJSONObject(j);
String matchID=innerObject.getString("matchID");
String matchUniqueID=innerObject.getString("matchUniqueID");
String team_one_name=innerObject.getString("team_one_name");
String team_two_name=innerObject.getString("team_two_name");
String dateTimeGmt=innerObject.getString("dateTimeGmt");
String match_type=innerObject.getString("match_type");
String match_dat=innerObject.getString("match_dat");
String is_done=innerObject.getString("is_done");
}
}
} catch (JSONException e) {
e.printStackTrace();
}