0

I am using Unity3D with Boomlagoon JSON from Unity asset store. I am sending a json from server and then extracting a string from that and comparing it with another string in an if statement. I cannot for the death of me make it work.

var reply : String = WebSocketSingleton.getInstance().RecvString();

Debug.Log ("Received: " + reply);

var jsonReply = JSONObject.Parse(reply);

Debug.Log("jsonReply['event']: " + jsonReply["event"].ToString());
Debug.Log("jsonReply['event'] type : " + jsonReply["event"].ToString().GetType());
Debug.Log("login type: " + "login".GetType());

if (jsonReply["event"].ToString() == "login") {
    Debug.Log("check");
} else {
    Debug.Log("fail");
}

It keeps going to else.

Console log:

Received: {"event":"login","email":"asdf@"}

jsonReply['event']: "login"

jsonReply['event'] type : System.String

login type: System.String

fail

Unity package I am using for json: https://www.assetstore.unity3d.com/en/#!/content/5788

I hope I'm not just being a massive idiot...

Waltari
  • 1,052
  • 1
  • 30
  • 64
  • 1
    try using `String.CompareTo()` method instead of `==` operator – Umair M Jul 24 '15 at 12:33
  • Okay that worked, but I would still like an explanation why `==` doesn't. Also why does `Debug.Log(jsonReply["event"].ToString().CompareTo("login"));` return `-1` and then the check after it passes as if it returned true. I am so confused. – Waltari Jul 24 '15 at 14:15

1 Answers1

1

I guess == doesn't work for strings.

Check This Tutorial for help.

Reference: Difference Between == and string.Equals

Community
  • 1
  • 1
Umair M
  • 10,298
  • 6
  • 42
  • 74