我有一个餐厅列表对象,并且该餐厅列表中还有一个美食列表对象。如何循环显示所有美食数据(新美式、日式、亚洲)。
public class RestaurantList {
@SerializedName("restaurant_id")
@Expose
private String restaurantId;
@SerializedName("restaurant_name")
@Expose
private String restaurantName;
@SerializedName("cuisine")
@Expose
private List<Cuisine> cuisine = null;
public List<Cuisine> getCuisine() {
return cuisine;
}
public void setCuisine(List<Cuisine> cuisine) {
this.cuisine = cuisine;
}
}
在Restaurant RVAdapter中,onBindViewHolder();
List<RestaurantList> mRestaurantList;
List<Cuisine> cuisineList = restaurantList.getCuisine();
String strCuisine = "";
for (int i = 0; i < cuisineList.size(); i++) {
strCuisine.concat(cuisineList.get(i).getCuisineName());
strCuisine.concat(",");
Log.i("Cuisine", cuisineList.get(i).getCuisineName());
holder.tv_restaurant_cuisine.setText(" " + cuisineList.get(i).getCuisineName());
}
Json 数组;
[
{...},
{
"restaurant_id": "41",
"restaurant_name": "Shwe Lar Food Restaurant",
"cuisine": [
{
"cuisine_name": "New American"
},
{
"cuisine_name": "Japanese"
},
{
"cuisine_name": "Asia"
}
],
}
]
最佳答案
使用以下方法解析您的数据: 我删除了数组后的最后一个和 {...}
private void parseJson(String jsonDataResponse){
try
{
JSONArray jsonArray = new JSONArray(jsonDataResponse);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String restaurant_id = jsonObject1.optString("restaurant_id");
String restaurant_name = jsonObject1.optString("restaurant_name");
JSONArray jsonArray1 =jsonObject1.getJSONArray("cuisine");
System.out.println("restaurant_id="+restaurant_id);
System.out.println("restaurant_name="+restaurant_name);
for(int j=0;j<jsonArray1.length();j++)
{
JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
String cuisine_name = jsonObject2.optString("cuisine_name");
System.out.println("cuisine_name="+cuisine_name);
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
关于java - 为什么java中无法提取Json数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575104/