如何从json格式字符串中获取特定数据以填充recyclerview?

时间:2017-09-16 02:08:52

标签: android json fetch implementation

今天我在理解如何从这个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值,veg或非veg配方。

4 个答案:

答案 0 :(得分: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.
  }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

试试这个

  

主要活动

  • 在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下载完整代码

答案 3 :(得分:0)

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

以下是如何获得&#34; veg&#34;的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。