Android: JSON string data is compared to another string

I would like to compare a row from mysql database using JSON with my row. I got the data from the database, and the log shows that I have all the data.

try{
     JSONArray jArray = new JSONArray(result);
     for(int i=0;i<jArray.length();i++){
         json_data = jArray.getJSONObject(i);
         Log.i("mylog","TABLE_NAME: "+json_data.getString("table_name"));//returns cars

         if (  json_data.getString("table_name") == "cars" ) { //dosent work
              .... 
         }
     }
} catch(JSONException e){
         Log.e("mylog", "Error parsing data "+e.toString());
     return false;
}

and in this example in the log I got the table name: cars And below the EU condition does not work, and I have no idea WHY. This is strange. I have tried many ways.

Do you know why I cannot compare table_name from JSON with a simple string?

+3
source share
1 answer

Use .equals()to compare two lines in java / android.

if (json_data.getString("table_name").equals("cars")) {  

         }

it compares Strings refrence, therefore does not work ...

when you use a string literal use ==

== String is Object .equals

+7

All Articles