Home  >  Article  >  Web Front-end  >  How does javascript determine whether it is in json format?

How does javascript determine whether it is in json format?

青灯夜游
青灯夜游Original
2021-06-07 16:34:0732731browse

Judgment method: First use the "JSON.parse(str)" statement to parse the specified data str; then use the "if(typeof obj =='object'&&obj)" statement to determine whether the type of the parsed data is " object" type; if so, the str data is in json format.

How does javascript determine whether it is in json format?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

js determines whether a string is in JSON format

Cannot be simply used to determine whether a string is in JSON format:

function isJSON(str) {
    if (typeof str == 'string') {
        try {
            JSON.parse(str);
            return true;
        } catch(e) {
            console.log(e);
            return false;
        }
    }
    console.log('It is not a string!')    
}

The above try/catch cannot completely verify that a string is a string in the JSON format. There are many exceptions:

JSON.parse('123'); // 123
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null

We know that in JS Data types are divided into: string, number, Boolean, array, object, Null, and Undefined.

We can use the following method to judge:

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!')
}


console.log('123 is json? ' + isJSON('123'))
console.log('{} is json? ' + isJSON('{}'))
console.log('true is json? ' + isJSON('true'))
console.log('foo is json? ' + isJSON('"foo"'))
console.log('[1, 5, "false"] is json? ' + isJSON('[1, 5, "false"]'))
console.log('null is json? ' + isJSON('null'))
console.log('["1{211323}","2"] is json? ' + isJSON('["1{211323}","2"]'))
console.log('[{},"2"] is json? ' + isJSON('[{},"2"]'))
console.log('[[{},{"2":"3"}],"2"] is json? ' + isJSON('[[{},{"2":"3"}],"2"]'))

The running result is:

> "123 is json? false"
> "{} is json? true"
> "true is json? false"
> "foo is json? false"
> "[1, 5, "false"] is json? true"
> "null is json? false"
> "["1{211323}","2"] is json? true"
> "[{},"2"] is json? true"
> "[[{},{"2":"3"}],"2"] is json? true"

So we draw the following conclusion:

JSON.parse() The method is used to parse JSON strings. json.parse() converts the string into a json object

If JSON.parse can be converted successfully; and the converted type is object and not equal to null, then This string is a string in JSON format.

JSON.parse():

JSON is usually used to exchange data with the server.

When receiving server data, it is usually a string.

We can use the JSON.parse() method to convert data into JavaScript objects.

Syntax:

JSON.parse(text[, reviver])

Parameter description:

  • text: required, a valid JSON string.

  • reviver: Optional, a function that converts the result. This function will be called for each member of the object.

Before parsing, make sure your data is in standard JSON format, otherwise parsing errors will occur.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How does javascript determine whether it is in json format?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn