“JavaScript Sprawdź, czy obiekt JSON jest prawidłowy” Kod odpowiedzi

Jeśli JSON VALIDE JS

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
Joyous Jaguar

JavaScript to prawidłowy ciąg JSON

function isValidJSONString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
//usage
var personJSONString = '{"first_name":"Tony","last_name":"Hawk","age":31}';
if(isValidJSONString(personJSONString)){
 //cool we are valid, lets parse
 var person= JSON.parse(personJSONString);
}
Grepper

JavaScript Sprawdź, czy obiekt JSON jest prawidłowy

//extensive check to make sure object is not of string type and not null
function isJson(item) {
    item = typeof item !== "string"
        ? JSON.stringify(item)
        : item;

    try {
        item = JSON.parse(item);
    } catch (e) {
        return false;
    }

    if (typeof item === "object" && item !== null) {
        return true;
    }

    return false;
}
Annoyed Albatross

Odpowiedzi podobne do “JavaScript Sprawdź, czy obiekt JSON jest prawidłowy”

Pytania podobne do “JavaScript Sprawdź, czy obiekt JSON jest prawidłowy”

Więcej pokrewnych odpowiedzi na “JavaScript Sprawdź, czy obiekt JSON jest prawidłowy” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu