如何从json格式字符串中获取特定数据以填充recyclerview?
今天我有问题了解如何从此json字符串中提取特定值。如何从json格式字符串中获取特定数据以填充recyclerview?
请注意,我打算使用从这里获取的数据填充回收站视图。
让我先告诉你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是否蔬菜或蔬菜的非的recyclerview取数据。
所以,请大家给我一些指导,告诉我如何在这种情况下基于recipe_filter值仅提取recipe_array值,veg或非veg食谱。你的“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值之后用蔬菜或蔬菜的非:
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.
}
}
尝试这个
主要活动
-
声明这上面你的onCreate方法
RecyclerView rv_myCart; Adapter adapter; ArrayList<HashMap<String,String>> all_list=new ArrayList<>();
-
的创建方法使用此代码
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); }
个适配器类别
-
申报您的onBindViewHolder这个方法在你的适配器类
private List<HashMap<String,String>> myCartList;
-
适配器方法
public Adapter(List<HashMap<String,String>> myCartList) { this.myCartList= myCartList; }
-
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 }
注意:-still从here
面临的问题下载完整的代码在给维沙尔Patolia的回答您的意见,您提到JSON查询。在没有任何库的Android上,访问JSON数据的选项是使用捆绑在Android平台中的org.json库。虽然您不能直接进行单个查询以获取所需的数据,但可以通过编程方式查询您的数据。
这里是你将如何得到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");
}
}
我建议你去维沙尔答案,并使用Gson库或类似的东西来分析你的JSON。
谢谢你推荐我的答案。 –
我正沿着使用JSON查询获取数据相应 –
@BodhisatwaChakraborty还可以使用改造的JSON解析的思路。 –