Android: парсинг JSON.Нет значения для ...? - PullRequest
0 голосов
/ 28 марта 2019

Я пытаюсь проанализировать данные JSON, сгенерированные API. Вот код, который я пытаюсь проанализировать:

    {  
   "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"
            }

ответ объект json внутри всего ответа:сбросив меняЯ не могу добраться до массива законодателей.LogCat дает мне эту ошибку: enter image description here

Как я могу изменить свой метод, чтобы получить массив 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...