I'm using a google API to get distance between two places and to check if I got distance information there's a status field on the json response for the API that can read "OK" or "NOT_AVAILABLE" or something along those lines. I want to check if the status is OK so I can use the distance info so I do it like this
if (disJSON["rows"][0]["elements"][0]["status"] == "OK")
The thing is that it returns false everytime even if the status is OK
I looked into the debugger to check the value and the value the debugger shows for the above element of the json is "\"OK\"". Which is weird. So I used that as the comparison string on the if statement like this
if (disJSON["rows"][0]["elements"][0]["status"] == "\"OK\"")
But it also returns false all the time. Any ideas how to compare this properly?
I suspect the problem is that the compile-time type of disJSON["rows"][0]["elements"][0]["status"]
is JToken
or something like that. If you cast to string, then a) you'll be comparing a string with a string; b) the compiler will use the bool ==(string, string)
overload instead of comparing for reference identity:
string status = (string) disJSON["rows"][0]["elements"][0]["status"];
if (status == "OK")
{
...
}
See more on this question at Stackoverflow