modabeckham
modabeckham

Reputation: 247

If condition does not work

I'm trying to get the value of entity which is inside json array. I have used the object subMenuObject to get and store the json response data. In my code I have used,

Log.i("MealDealCatCode ", subMenuObject.getString("MealDealCatCode"));

and it prints L3 in my logcat.

My problem is I have used a if condtion as,

if ((subMenuObject.getString("MealDealCatCode")).equals("L3")) {

When I debug and try to go inside the if condition it doesn't go in. It check that and skip the if condition. What is the problem in my if condition. Any help will be highly appreciated.

code used to access the value

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

    JSONArray subMenuArray = object.getJSONArray("MealDealItemEntity");

    for (int j = 0; j < subMenuArray.length(); ++j) {
        JSONObject subMenuObject = subMenuArray.getJSONObject(j);

        Log.i("MealDealCatCode ", subMenuObject.getString("MealDealCatCode"));

        if ((subMenuObject.getString("MealDealCatCode")).equals("L3")) {
            Log.i("MainMenuDescription", subMenuObject.getString("MainMenuDescription"));
        }
    }
}

json response:

{  
     "MealDealItemEntity":[  
         {  
            "MealDealCatCode":"L3  ",
            "MainMenuDescription":"Jumbo Coca Cola 199 Rs. 0.00",
         }
      ],
      "MealDealCatCode":"L3  "
}

Upvotes: 0

Views: 66

Answers (2)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Because it contains spaces.

Try this instead:

if (subMenuObject.getString("MealDealCatCode").trim().equals("L3")) {

The trim() function removes the spaces.

Upvotes: 1

malarres
malarres

Reputation: 2946

It looks like you need to add a .trim()

Upvotes: 1

Related Questions