我正在尝试解析API生成的JSON数据,这是我正在尝试解析的json:
{
"response":{
"legislator":[
{
"@attributes":{
"cid":"N00033987",
"firstlast":"Doug LaMalfa",
"lastname":"LAMALFA",
"party":"R",
"office":"CA01",
"gender":"M",
"first_elected":"2012",
"exit_code":"0",
"comments":"",
"phone":"202-225-3076",
"fax":"530-534-7800",
"website":"http:\/\/lamalfa.house.gov",
"webform":"https:\/\/lamalfa.house.gov\/contact\/email-me",
"congress_office":"322 Cannon House Office Building",
"bioguide_id":"L000578",
"votesmart_id":"29713",
"feccandid":"H2CA02142",
"twitter_id":"RepLaMalfa",
"youtube_url":"https:\/\/youtube.com\/RepLaMalfa",
"facebook_id":"RepLaMalfa",
"birthdate":"1960-07-02"
}
整个响应中的 response json对象让我失望。我不能去立法者数组。 logcat给了我这个错误:
如何修改我的方法以获取立法者json数组? 这是我的解析方法:
private void getData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject responseObj = jsonObject.getJSONObject("response");
JSONArray array = responseObj.getJSONArray("legislator");
for(int i = 0; i < array.length(); i++){
JSONObject attributesObj = array.getJSONObject(i);
//create object
Legs leg = new Legs(attributesObj.getString("firstlast"),
attributesObj.getString("party"),
attributesObj.getString("office"),
attributesObj.getString("gender"),
attributesObj.getString("birthdate"),
attributesObj.getString("first_elected"),
attributesObj.getString("phone"),
attributesObj.getString("website"),
attributesObj.getString("congress_office"),
attributesObj.getString("twitter_id"),
attributesObj.getString("youtube_url"),
attributesObj.getString("facebook_id"));
legList.add(leg);
}
adapter = new LegRvAdapter(Legislators.this, legList);
myrv.setAdapter(adapter);
}catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Volley", volleyError.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}