Js determined whether JSON format string

function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
                return true;
            }else{
                return false;
            }

        } catch(e) {
            console.log('error:'+str+'!!!'+e);
            return false;
        }
    }
    console.log('It is not a string!')
}

var obj = JSON.parse (p)

typeof obj == 'object' && obj

Try catch cover with an error.

Guess you like

Origin www.cnblogs.com/xulei1992/p/12010581.html