JS 验证字符串是否能转为json格式

var isJSON=function (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!')
            }

返回值为true/false

--->【最简单的方法】js判断字符串是否为JSON格式(20180115更新)

原文地址:https://www.cnblogs.com/lbonet/p/11572819.html