安卓工作室 : How to parse a JSON Object from Inside of a JSON Array?

标签 android arrays json

我正在尝试将以下 JSON 数据解析为回收器 View 。特别是,我想解析地址对象以显示 locationName、line1、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 类的代码是正确的,错误出在这个方法中,但如果您想查看任何其他代码,请告诉我。

最佳答案

通过获取数组中的第一个节点,您已经获得了 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");
            }
        }

关于安卓工作室 : How to parse a JSON Object from Inside of a JSON Array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080402/

相关文章:

python - 如何在 python 中将 2d 列表分成两个单独的列表?

javascript - 无法解析 AngularJS 中的特定 JSON 数据

ios - 将 UNIX 时间从 json 导入(swift 结构)转换为日期作为字符串并填充表

从数据库检索时,PHP 显示奇怪的条目

java - 调用 Intent Activity 后如何修复应用程序崩溃

android - 如何在 listView 中添加带有进度条(微调器)的 subview ?

android - Jetpack Compose UI - 在 AlertDialog 内单击时按钮宽度发生变化

c++ - 用数组初始化一个类

PHP - 迭代文件并分解 JSON 文本 block

android - 在 Android 数据绑定(bind)中使用长值