Android Studio:如何从JSON数组内部解析JSON对象?

时间:2019-01-07 19:12:19

标签: android arrays json

我正在尝试将以下JSON数据解析到回收器视图中。特别是,我想解析地址对象以显示locationName,line1,line,city,state和zip,但是我无法正确获取解析方法。

   {
 "pollingLocations": [
  {
   "address": {
    "locationName": "LITTLE LEAGUE HEADQUARTERS",
    "line1": "6707 LITTLE LEAGUE DR",
    "city": "SAN BERNARDINO",
    "state": "CA",
    "zip": "92407"
   },
   "notes": "",
   "pollingHours": "07:00-20:00",
   "sources": [
    {
     "name": "Voting Information Project",
     "official": true
    }
   ]
  }
 ]
}

我的问题是找到地址对象。使用当前代码,我得到一个logcat错误,提示locationName没有值。我认为这是因为我首先需要指定要迭代的地址对象,但是我该怎么做呢?这是我现在拥有的当前实现。

//fetch
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);
                JSONArray array = jsonObject.getJSONArray("pollingLocations");
                for(int i = 0; i < array.length(); i++){
                    JSONObject addressObject = array.getJSONObject(i);
                    for (int x = 0; x < addressObject.length(); x++){
                        VotingLoc votingLoc = new VotingLoc(addressObject.getString("locationName"),
                                addressObject.getString("line1"),
                                addressObject.getString("city"),
                                addressObject.getString("state"),
                                addressObject.getString("zip"),
                                addressObject.getString("pollingHours"));
                        votingList.add(votingLoc);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

定位错误: enter image description here

有没有更简单的方法?我相信我的rvadapter类的代码是正确的,并且此方法中有错误,但是如果您想查看其他任何代码,请告诉我。

2 个答案:

答案 0 :(得分:1)

通过获取数组中的第一个节点,您已经获得了address节点,因此无需在此处运行loopaddress对象不是可以直接获取值的数组。尝试这样的事情:

        JSONArray array = object.getJSONArray("pollingLocations");
        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
           String locationName = jsonObject.getJSONObject("address")
                   .getString("locationName");
           Log.d("locationName", locationName);
            JSONArray sourcesArray = jsonObject.getJSONArray("sources");

          // run loop to get values from this array
          for (int j = 0; j < sourcesArray.length(); j++){
                JSONObject source = sourcesArray.getJSONObject(j);
                String name = source.getString("name");
            }
        }

答案 1 :(得分:0)

另一种方法是使用json解析器,例如gson(https://github.com/google/gson)或jackson(https://github.com/FasterXML/jackson)。如果您的唯一目标是显示这几个值,那么这可能会有些开销,因为您必须创建一些Java类,所以可能只需要解析。