如何在 android recyclerview 中从服务器访问 json 文件的子文件和子文件

Posted

技术标签:

【中文标题】如何在 android recyclerview 中从服务器访问 json 文件的子文件和子文件【英文标题】:How to access childs and sub childs of json file from server in android recyclerview 【发布时间】:2020-10-14 06:01:36 【问题描述】:

我想从存储在服务器上的 json 文件中访问数组子元素和子子元素,并在 recyclerview 中显示它们我尝试了很多方法,但没有一个对我有用,因为我不了解这个库

    
  "category1": 
    "subcategory1": 
      "products": [
        
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        
      ]
    ,
    "subcategory2": 
      "products": [
        
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        
      ]
    
  ,
  "category2": 
    "subcategory1": 
      "products": [
        
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        
      ]
    ,
    "subcategory2": 
      "products": [
        
          "title": "Air Fresheners",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Cloth Detergent",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        ,
        
          "title": "Milk",
          "image": "https://ludobets.in/atma_nirbhar/my_private_secure_icons/secure_home_icons/ic_apps_scan.png"
        
      ]
    
  

我在不了解它们的情况下尝试了很多方法,但没有一个对我有用。 它给出了如下所示的错误

of type org.json.JSONObject cannot be converted to JSONArray

我的代码是

private void getData() 
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() 
        @Override
        public void onResponse(JSONArray response) 


            for (int i = 0; i < response.length(); i++) 
                try 

                    JSONObject jsonObjectRoot = response.getJSONObject("category1");
                    JSONObject jsonObjectSub = jsonObjectRoot.getJSONObject("subcategory1");
                    JSONArray jsonArrayForSub = jsonObjectSub.getJSONArray("products");
                    JSONObject eachObjectInArray = jsonArrayForSub.getJSONObject(i);

                    Note note = new Note();
                    note.setTitle(eachObjectInArray.getString("title"));
                    note.setImage(eachObjectInArray.getString("image"));

                    noteList.add(note);
                 catch (JSONException e) 
                    e.printStackTrace();
                    progressDialog.dismiss();
                
            
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();
        
    , new Response.ErrorListener() 
        @Override
        public void onErrorResponse(VolleyError error) 
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        
    );
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);

【问题讨论】:

如果我对 JSON 的了解正确,子类别不是 JSON 数组,而是一个对象,此实例中的 JSON 数组是产品 上面的代码只是我的代码示例,而上面的 json 实际上是我想用于我的目的 是的,在您的代码中,您说的是 jsonArray = jsonObject.getJSONArray("subcategory"),但这是不正确的,因为子类别(在您的 json 中)不是数组 谢谢,请帮帮我,我搜索了很多,但什么也没找到 尝试通过此链接阅读:***.com/questions/41928314/… 【参考方案1】:

改为使用retrofit从服务器获取json并转换为模型类结构列表,这里是参考 链接:https://square.github.io/retrofit/

【讨论】:

以上是关于如何在 android recyclerview 中从服务器访问 json 文件的子文件和子文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studio 中禁用 recyclerview 特定数据?

如何在 Android 的 RecyclerView 中显示来自 Firestore 的数据?

如何在 recyclerview 适配器中将动态视图添加到 android 布局

如何在android中排列RecyclerView上的数据?

如何在android的recyclerview中搜索数据

如何在 Android Studio 中同时从 recyclerview 和数据库中删除数据? [关闭]