Skip to content

判断是否json

jerryzhang edited this page Jan 31, 2023 · 1 revision

title: 判断是否json url: https://www.yuque.com/endday/blog/mupa4g

function isJson (str) {
    if (typeof str !== 'string') {
        return false
    }
    const char = str.charAt(0)
    if (char !== '[' && char !== '{') {
        return false
    }
    try {
        return typeof JSON.parse(str) === 'object'
    } catch (e) {
        return false
    }
}
Clone this wiki locally