0

I'm using the below code to take the values of price. I can get the value for price. However, when it comes to crust it runs to the exception.

@Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> crust = new ArrayList<String>();   
            List<String> price = new ArrayList<String>();

            for (int i = 0; i < responseJson.length(); ++i) {
                JSONObject object = responseJson.getJSONObject(i);

                if ((object.getString("MainCategoryID")).equals("1")
                        && (object.getString("SubCategoryID")).equals("1")) {
                    Log.i("Price ", object.getString("Price"));
                    price.add(object.getString("Price"));
                    Log.i("Crust ", object.getString("Crust"));
                    crust.add(object.getString("Crust"));

                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

this is the json response

{  
      "Category":"1PI",
      "Price":0.0000,
      "SubCategoryID":1,
      "SubMenu":true,
      "SubMenuEntity":[  
         {  
            "Crust":"Sausage",

Crust is positioned in, array inside an array, how can i access crust in my above coding.

any help will be appreciated.

2
  • "Crust is positioned in, array inside an array" : No, Crust is in a JSON object in a JSON array which is in a JSON object. All you need to do is get the value of the SubMenuEntity (the JSON array) and get the JSON object(s) it contains and then get the value of the Crust name/value pair. Commented Feb 25, 2015 at 14:32
  • Get jsonArray from your object and get Crust from that array. I'd reccomend you to use GSON on parsing json responses, also it's better to use a JSON library in your project like retrofit both for performance & manageability. Commented Feb 25, 2015 at 14:32

2 Answers 2

1

SubMenuEntity item has an array of objects. You need to get the array and loop though it. For each object, get the value of Crust.

try {
      List<String> crust = new ArrayList<String>();
      List<String> price = new ArrayList<String>();

      JSONArray responseJson = null;

      for (int i = 0; i < responseJson.length(); ++i) {
        JSONObject object = responseJson.getJSONObject(i);

        if ((object.getString("MainCategoryID")).equals("1")
            && (object.getString("SubCategoryID")).equals("1")) {
          Log.i("Price ", object.getString("Price"));
          price.add(object.getString("Price"));

          JSONArray subMenuArray = object.getJSONArray("SubMenuEntity");
          for (int j = 0; j < subMenuArray.length(); ++j) {
            JSONObject subMenuObject = subMenuArray.getJSONObject(j);
            Log.i("Crust ", subMenuObject.getString("Crust"));
            crust.add(subMenuObject.getString("Crust"));
          }

        }

      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

your Crust is inside SubMenuEntity, you shoud get this using object.getJSONArray("SubMenuEntity") and then get Crust from first Object in this array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.