a very strange action after using getJson function of Jquery

a very strange action after using getJson function of Jquery

Dear All,
 
I found a very a very strange action after using getJson function 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;
        })
    });






//the result of below line is alway false even these two values are same, if use alert function before it, it will work well, hope anyone can help me:)
        if (o.val().toLowerCase() != randomcode.toLowerCase()) {
            o.addClass("ui-state-error");          
            return false;
        } else {



            return true;
        }
    }