a stange result after using getJson of Jquery

a stange result after using getJson of Jquery

I found a  stange result after using getJson of Jquery, pls see code as below:
function checkCode(o) {
        var randomcode = "";
        $.getJSON(
    "/Handler/GetCodeHandler.ashx",  function (json) {
        $.each(json, function (i) {
            randomcode = json[i].code;
        })
    });






//after getting the code through getJson, the result of below line is always true even if the two values are same... but if we add alert() before it, then it can work well, I am waitting for help:)
        if (o.val().toLowerCase() != randomcode.toLowerCase()) {
            o.addClass("ui-state-error");       
            return false;
        } else {



            return true;
        }
    }