【问题标题】:How to fetch a specific data from a json format string to populate recyclerview?如何从 json 格式字符串中获取特定数据以填充 recyclerview?
【发布时间】:2017-09-16 02:08:51
【问题描述】:

今天我在理解如何从这个 json 字符串中提取特定值时遇到问题。

请注意,我打算使用从此处获取的数据填充回收站视图。

先给大家看一下json字符串的格式:

[
{
"recipe_filter":"Veg",
"recipe_array":[
{
"recipe_name":"Nimki",
"recipe_image":"nimki",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"2 Hours",
"recipe_prep_steps":"Steps",
"recipe_ingredients":"123456"
},
{
"recipe_name":"Palang Paneer",
"recipe_image":"palang_paneer",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"1.5 Hours",
"recipe_prep_steps":"Steps",
"recipe_ingredients":"12"
}
]
},
{
"recipe_filter":"Non Veg",
"recipe_array":[
{
"recipe_name":"Chicken Rezala",
"recipe_image":"chicken_rezala",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"2 Hours",
"recipe_prep_steps":"1234567",
"recipe_ingredients":"123"
},
{
"recipe_name":"Omu Rice",
"recipe_image":"omu_rice",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"1 Hour",
"recipe_prep_steps":"123456",
"recipe_ingredients":"12"
}
]
},
{
"recipe_filter":"",
"recipe_array":[
{
"recipe_name":"",
"recipe_image":"",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"",
"recipe_prep_steps":"",
"recipe_ingredients":""
},
{
"recipe_name":"",
"recipe_image":"",
"isFavourite":false,
"isInCookList":false,
"recipe_prep_time":"",
"recipe_prep_steps":"",
"recipe_ingredients":""
}
]
}
]

我想根据 recipe_filter 是 veg 还是 non veg 来获取 recyclerview 中的数据。

所以伙计们,请给我一些指导,告诉我在这种情况下如何根据 recipe_filter 值仅获取 recipe_array 值、蔬菜或非蔬菜食谱。

【问题讨论】:

    标签: android json fetch implementation


    【解决方案1】:

    首先获取“recipe_filter”的值,然后与您的值进行比较,请检查以下代码:

    首先创建json响应POJO类:

    public class RecipeResponse {
    
    @SerializedName("recipe_filter")
    @Expose
    private String recipeFilter;
    @SerializedName("recipe_array")
    @Expose
    private List<RecipeArray> recipeArray = null;
    
    public String getRecipeFilter() {
    return recipeFilter;
    }
    
    public void setRecipeFilter(String recipeFilter) {
    this.recipeFilter = recipeFilter;
    }
    
    public List<RecipeArray> getRecipeArray() {
    return recipeArray;
    }
    
    public void setRecipeArray(List<RecipeArray> recipeArray) {
    this.recipeArray = recipeArray;
    }
    
    }
    

    在获得成功的 json 响应后将你的 recipe_filter 值与 veg 或 non veg 进行比较:

    public void doRecipeSuccess(RecipeResponse mRecipeResponse){
        if(mRecipeResponse.recipeFilter.equal("veg")){
    // Write down veg code.
    }else if(mRecipeResponse.recipeFilter.equal("non veg")){
    // Write down non veg code.
      }
    }
    

    【讨论】:

    • 我正在考虑使用 json 查询来相应地获取数据
    • @BodhisatwaChakraborty 也使用 Retrofit 进行 JSON 解析。
    【解决方案2】:

    创建 POJO 类,然后使用 GSON 库a link

    【讨论】:

      【解决方案3】:

      在您对 Vishal Patolia 的回答的评论中,您提到了 JSON 查询。在没有任何库的 Android 上,访问 JSON 数据的一个选项是使用 Android 平台中捆绑的 org.json 库。虽然您无法直接进行单个查询来获取所需的数据,但您可以通过编程方式查询您的数据。

      以下是获取“veg”的 recipe_array 的方法:

      JSONArray json = new JSONArray(jsonString); 
      for (int i = 0; i < json.length(); i++) {
          if (json.getJSONObject(i).getString("recipe_filter").equals("veg")) {
              // Here is one recipe_array for veg
              JSONArray someVegArrayData = json.getJSONObject(i).getJSONArray("recipe_array");
          }
      }
      

      我建议您使用 Vishal 的答案并使用 Gson 库或类似的东西来解析您的 JSON。

      【讨论】:

      • 感谢您推荐我的回答。
      【解决方案4】:

      试试这个

      主要活动

      • 在 oncreate 方法的上面声明这个

        RecyclerView rv_myCart;
        Adapter adapter;
        ArrayList<HashMap<String,String>> all_list=new ArrayList<>();
        
      • 在您的 on create 方法中使用此代码

        rv_myCart=(RecyclerView)findViewById(R.id.rv_completepurchase);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        rv_myCart.setLayoutManager(mLayoutManager);
        rv_myCart.setItemAnimator(new DefaultItemAnimator());
        adapter = new Adapter(all_list);
        rv_myCart.setAdapter(adapter);
            try {
                JSONArray json=new JSONArray(strjson);
        //                Log.e(TAG, "json: "+json );
                for (int i=0;i<json.length();i++)
                {
                    HashMap<String,String> hashMap=new HashMap<>();
                    JSONObject jsonobject=json.getJSONObject(i);
        //                    Log.e(TAG, "recipe_filter: "+jsonobject.getString("recipe_filter") );
                    hashMap.put("recipe_filter",jsonobject.getString("recipe_filter"));
                    JSONArray recipe_array=jsonobject.getJSONArray("recipe_array");
               //                    Log.e(TAG, "recipe_array: "+recipe_array );
                    for (int j=0;j<recipe_array.length();j++)
                    {
                        JSONObject object=recipe_array.getJSONObject(j);
              //                        Log.e(TAG, "recipe_name: "+object.getString("recipe_name") );
                        hashMap.put("recipe_name",object.getString("recipe_name") );
                    }
                    all_list.add(hashMap);
                }
                adapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e(TAG, "e: "+e );
            }
        

      适配器类

      • 在你的适配器类中声明这个

         private List<HashMap<String,String>> myCartList;
        
      • 你的适配器方法

         public Adapter(List<HashMap<String,String>> myCartList) {
        this.myCartList= myCartList;
        }
        
      • 在你的 onBindViewHolder 方法中

        HashMap<String,String> hashmap=myCartList.get(position);
        if (hashmap.get("recipe_filter").equals("Veg"))
        {
            //todo when recipe_filter is veg
        }
        else
        {
            //// TODO: when recipe_filter is nonveg 
        }
        

      注意:-仍然面临从here下载完整代码的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        • 2020-03-17
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多