uTools-Manuals/docs/jQuery/jQuery.parseJSON.html
2019-04-21 11:50:48 +08:00

58 lines
3.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.parseJSON()</h1>
<div class="entry-meta">
所属分类:<span class="category"><a href="/category/utilities/">工具类</a></span>
<span class="pull-right">英文文档:<a href="https://api.jquery123.com/jQuery.parseJSON/" target="_blank">jQuery.parseJSON()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-parseJSON1"><h2 class="section-title">
<span class="name">jQuery.parseJSON( json )</span><span class="returns">返回: <a href="/Types/#Object">Object</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>接受一个标准格式的 JSON 字符串,并返回解析后的 JavaScript 对象。</p>
<ul class="signatures"><li class="signature" id="jQuery-parseJSON-json">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4.1/">1.4.1</a></span>jQuery.parseJSON( json )</h4>
<ul><li>
<div><strong>json</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>要解析的 JSON 字符串。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>传入格式有误的 JSON 字符串可能导致抛出异常。例如,下面这些 JSON 字符串格式都不对:</p>
<ul>
<li>
<code>{test: 1}</code> (test 没有使用双引号包裹).</li>
<li>
<code>{'test': 1}</code> ('test' 用了单引号而不是双引号包裹).</li>
</ul>
<p>
JSON标准不允许“控制字符”如制表符或换行符。比如<code>$.parseJSON('{"testing":"1\t2\n3"}')</code>大多数实现中将抛出一个错误因为JavaScript分析器直接转换字符串的制表符和换行符为文本的制表符和换行符;产生双反斜杠,例如<code>"1\\t2\\n3"</code>是预期的结果。这个问题往往在服务器端语言如PHPJSON注入到一个JavaScript文件时发生。</p>
<p>JSON.parse jQuery 则会使用它来解析字符串。更多关于JSON格式的细节请参考 <a href="http://json.org/">http://json.org/</a></p>
<p>在jQuery 1.9之前,如果传递给<code>$.parseJSON</code>一个空字符串,<code>null</code>, 或者 <code>undefined</code>,,将返回<code>null</code>而不是抛出一个错误即使这些都不是有效的JSON。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">解析一个 JSON 字符串。</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> obj = jQuery.parseJSON(<span class="string">'{"name":"John"}'</span>);</code></div></div><div class="container"><div class="line"><code>alert( obj.name === <span class="string">"John"</span> );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>