0

I have some json like [{"person":"123abc"}] in variable JSON_STRING i try parse json to string like this

    JSONObject person = (new JSONObject(JSON_STRING));
    String name = person.getString("person");
    String test= "123abc";
    if(name == test){
       System.out.print("Success");
    }else{
       System.out.print("Fail");
    } 

I make some simple logic with if else but i have some problem variable name and testnot same because result Fail. I don't know why content variable name and test considered not same. Please help if anyone have some solution.

Thanks

Nugka
  • 301
  • 2
  • 15

2 Answers2

2

Here:

if(name == test){

You compare references not values. Use :

if(name.equals(test)){
marcinj
  • 48,511
  • 9
  • 79
  • 100
1

if(name == test){ should be if(name.equals(test)){

==is used to compare references, and equals is using to compare values

Stéphane Ammar
  • 1,454
  • 10
  • 17