Home  >  Article  >  Web Front-end  >  How does js determine that a string is in JSON format?

How does js determine that a string is in JSON format?

零下一度
零下一度Original
2017-06-30 13:18:382121browse

Preface

Regarding the problem of "js determines whether a string is in JSON format", I checked a lot of information on the Internet, but I didn't find the answer I wanted.

But after seeing this post"js determines whether a string is in JSON format", suddenly an idea flashed and I thought of a very Simple solution.

If you have any objections to this method, please leave a message to discuss.

Solution

function isJSON(str) {if (typeof str == 'string') {try {var obj=JSON.parse(str);if(str.indexOf('{')>-1){return true;
                }else{return false;
                }

            } catch(e) {
                console.log(e);return false;
            }
        }return false;
}

Analysis

Like the previous post said, just simply useJSON.parse(str) cannot completely verify that a string is a string in the JSON format, there are many exceptions:

<span class="hljs-built_in">JSON.parse(<span class="hljs-string">'123'); <span class="hljs-comment">// 123</span></span></span>

<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"> JSON.parse(<span class="hljs-string">'{}'); <span class="hljs-comment">// {}</span></span></span></span></span></span>

<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"> JSON.parse(<span class="hljs-built_in">'true'); <span class="hljs-string">// true<span class="hljs-comment"></span></span></span></span></span></span></span></span></span>

<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"> JSON.parse(<span class="hljs-comment">'"foo"' ); <span class="hljs-built_in">// "foo"<span class="hljs-string"><span class="hljs-comment"></span></span></span></span></span></span></span></span></span></span></span> </span>

<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"> <span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"> JSON.parse(<span class="hljs-string">'[1, 5, "false"]'); <span class="hljs-comment">// [1, 5, "false"]<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"> JSON.parse(<span class="hljs-comment">'null'); <span class="hljs-built_in">// null<span class="hljs-string"><span class="hljs-comment"><span class="hljs-built_in"><span class="hljs-string"><span class="hljs-comment"></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>                                                                                                                                                                                                                                             Null, Undefined.

Then we will test these types of strings.

   

<span style="font-size: 16px;"><span style="color: #0000ff;">function</span><span style="color: #000000;"> isJSON_test(str) {</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">typeof</span> str == 'string'<span style="color: #000000;">) {</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {</span><span style="color: #0000ff;">var</span> obj=<span style="color: #000000;">JSON.parse(str);
                console.log(</span>'转换成功:'+<span style="color: #000000;">obj);</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
            } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(e) {
                console.log(</span>'error:'+str+'!!!'+<span style="color: #000000;">e);</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
            }
        }
        console.log(</span>'It is not a string!'<span style="color: #000000;">)
 }</span><em> <br></em></span>
<span style="font-size: 16px;">isJSON_test('123'); //number</span><br><span style="font-size: 16px;">isJSON_test('aaaa'); //string</span><br><span style="font-size: 16px;">isJSON_test('"aaa"');</span><br><span style="font-size: 16px;">isJSON_test('true'); //布尔</span><br><span style="font-size: 16px;">isJSON_test('["1","2"]'); //数组</span><br><span style="font-size: 16px;">isJSON_test('{name:"123"}'); //对象</span><br><span style="font-size: 16px;">isJSON_test('{}'); //空对象</span><br><span style="font-size: 16px;">isJSON_test('null'); //null</span><br><span style="font-size: 16px;">isJSON_test('Undefined'); //Undefined</span><br><span style="font-size: 16px;">isJSON_test('{"name":"123"}'); //json</span><br><span style="font-size: 16px;"><em>isJSON_test('{"name":"123",}'); //不规范的json</em></span>

 

  The test results are as follows:

 

 

  From the above test results, and Definition of JSON. We can get a rule:

If JSON.parse can be converted successfully; and the string contains {, then the string is a string in JSON format.

If you have any objections to this method, please leave a message below, thank you.

The above is the detailed content of How does js determine that a string 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