Android 如何通过截取在数组中提取这个json数组?

Android 如何通过截取在数组中提取这个json数组?,android,json,android-volley,Android,Json,Android Volley,我试图从Json对象中提取数据列表数组,但我不知道该怎么做。这是我试图提取的成分的价值。我试图将其提取为字符串,但结果未格式化。我还在这里提供了原始JSoneter代码的图像。如果我试图将其提取为'cake.optJSONArray',我会得到一个java.lang.ClassCastException:org.json.JSONArray无法转换为java.util.List 我添加了更多的代码来帮助这个问题变得有意义,但愿我没有让它变得更混乱 try {

我试图从Json对象中提取数据列表数组,但我不知道该怎么做。这是我试图提取的成分的价值。我试图将其提取为字符串,但结果未格式化。我还在这里提供了原始JSoneter代码的图像。如果我试图将其提取为'cake.optJSONArray',我会得到一个java.lang.ClassCastException:org.json.JSONArray无法转换为java.util.List 我添加了更多的代码来帮助这个问题变得有意义,但愿我没有让它变得更混乱

   try {

                for (int i = 0; i < response.length(); i++) {
                    JSONObject cake = response.getJSONObject(i);

                    String cakeId = cake.optString("id");
                    String cakeName = cake.optString("name");


                    List<Ingredients> ingredients = (List<Ingredients>) cake.optJSONArray("ingredients");

                    mCakeList.add(new CakesItem(cakeId,ingredients, cakeName));
                }

我不确定您使用的是什么JSON库,但是如果您导入org.JSON.JSONArray,那么如果您的蛋糕是一个org.JSON.JSONObject,您从截取返回的JSONObject响应中得到的,那么您将使用以下语法:

JSONObject cake = response.getJSONObject(i);
JSONArray ingredients = cake.getJSONArray("ingredients");
然后,由您将JSONArray成分中的值放入列表中。它看起来像这样:

for (int i = 0; i < response.length(); i++) {
    JSONObject cake = response.getJSONObject(i);

    String cakeId = cake.optString("id");
    String cakeName = cake.optString("name");

    JSONArray JSONingredients = cake.optJSONArray("ingredients");

    List<Ingredients> ingredients = new List<Ingredients>();

    for (int j = 0; j < JSONingredients.length(); j++) {
        JSONObject item = JSONingredients.getObject(j);

        String measure = item.getString(“measure”);
        String ingredient = item.getString(“ingredient”);
        Double quantity = item.getDouble(“quantity”);

        Ingredients item2 = new Ingredients(measure, ingredient, quantity);
        ingredients.add(item2);
      } 


      mCakeList.add(new CakesItem(cakeId,ingredients, cakeName));
    }

哦,是的,这是可行的,但它仍然会生成未格式化的文本,因为在JSONArray成分中有一个完整的对象数组,这些对象是数量、度量和成分,我想要格式化这些值,因此我最初创建了两个对象CakeItem和IngredientsThank,谢谢你,我只需将列表转换为ArrayList。
JSONObject cake = response.getJSONObject(i);
JSONArray ingredients = cake.getJSONArray("ingredients");
for (int i = 0; i < response.length(); i++) {
    JSONObject cake = response.getJSONObject(i);

    String cakeId = cake.optString("id");
    String cakeName = cake.optString("name");

    JSONArray JSONingredients = cake.optJSONArray("ingredients");

    List<Ingredients> ingredients = new List<Ingredients>();

    for (int j = 0; j < JSONingredients.length(); j++) {
        JSONObject item = JSONingredients.getObject(j);

        String measure = item.getString(“measure”);
        String ingredient = item.getString(“ingredient”);
        Double quantity = item.getDouble(“quantity”);

        Ingredients item2 = new Ingredients(measure, ingredient, quantity);
        ingredients.add(item2);
      } 


      mCakeList.add(new CakesItem(cakeId,ingredients, cakeName));
    }