1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
---
title: JSON.parse()
slug: Web/JavaScript/Reference/Global_Objects/JSON/parse
tags:
- ECMAScript5
- JSON
- Method
- Referensi
translation_of: Web/JavaScript/Reference/Global_Objects/JSON/parse
---
<div>{{JSRef}}</div>
<p>Method <strong><code>JSON.parse()</code></strong> memparse string ke JSON, biasanya mengubah nilai yang dihasilkan oleh parsing.</p>
<h2 id="Sintaks">Sintaks</h2>
<pre class="syntaxbox"><code>JSON.parse(<var>text</var>[, <var>reviver</var>])</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>text</code></dt>
<dd>String yang akan di parse ke JSON. Lihat objek {{jsxref("JSON")}} untuk deskripsi lengkap sintaks JSON.</dd>
<dt><code>reviver</code> {{optional_inline}}</dt>
<dd>Jika sebuah fungsi, mendeskripsikan bagaimana nilai asli dihasilkan oleh parsing berubah, sebelum dikembalikan.</dd>
</dl>
<h3 id="Pemulangan">Pemulangan</h3>
<p>Mengembalikan {{jsxref("Object")}} sesuai dengan teks JSON yang diberikan.</p>
<h3 id="Lontaran">Lontaran</h3>
<p>Melontarkan pengecualian {{jsxref("SyntaxError")}} jika string yang di parse tidak sesuai JSON.</p>
<h2 id="Contoh">Contoh</h2>
<h3 id="Menggunakan_JSON.parse()">Menggunakan <code>JSON.parse()</code></h3>
<pre class="brush: js">JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
</pre>
<h3 id="Menggunakan_parameter_reviver">Menggunakan parameter <code>reviver</code></h3>
<p>Jika <code>reviver</code> telah ditentukan, nilai dihitung dengan parsing <em>transformed</em> sebelum dikembalikan. Khususnya, nilai yang terhitung, dan semua propertinya (dimulai dari properti yang paling bersarang dan berlanjut ke nilai asli), secara individu dijalankan melalui <code>reviver</code>, yang di panggil dengan objek yang berada di dalam properti yang akan diproses sebagai <code>this</code> dan dengan nama properti sebagai string dan nilai properti sebagai argumen. jika fungsi <code>reviver</code> mengembalikan {{jsxref("undefined")}} (tidak mengembalikan nilai, contoh, jika eksekusi gagal di akhir fungsi), properti akan dihapus dari objek. Jika tidak, properti akan didefinisikan ulang sebagai sebuah nilai kembali.</p>
<p><code>reviver</code> dipanggil dengan string kosong dan nilai paling atas mengizinkan tranformsi dari nilai paling atas. Jadi untuk menangani kasus ini dengan baik, biasanya dengan mengembalikan nilai yang diberikan, atau <code>JSON.parse()</code> akan mengembalikan {{jsxref("undefined")}}.</p>
<pre class="brush: js">JSON.parse('{"p": 5}', function(k, v) {
if (k === '') { return v; } // if topmost value, return it,
return v * 2; // else return v * 2.
}); // { p: 10 }
JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', function(k, v) {
console.log(k); // log the current property name, the last is "".
return v; // return the unchanged property value.
});
// 1
// 2
// 4
// 6
// 5
// 3
// ""
</pre>
<h3 id="JSON.parse()_tidak_mengizinkan_trailing_koma"><code>JSON.parse()</code> tidak mengizinkan trailing koma</h3>
<pre class="example-bad brush: js">// both will <code>throw a SyntaxError</code>
JSON.parse('[1, 2, 3, 4, ]');
JSON.parse('{"foo" : 1, }');
</pre>
<h2 id="Spesifikasi">Spesifikasi</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spesifikasi</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.12.2', 'JSON.parse')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.7.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-json.parse', 'JSON.parse')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-json.parse', 'JSON.parse')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Kompabilitas_browser">Kompabilitas browser</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Fitur</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Dukungan dasar</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("1.9.1")}}</td>
<td>{{CompatIE("8.0")}}</td>
<td>{{CompatOpera("10.5")}}</td>
<td>{{CompatSafari("4.0")}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Fitur</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Dukungan dasar</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("1.0")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Gecko-specific_notes">Gecko-specific notes</h2>
<p>Pada Gecko 29 {{geckoRelease("29")}}, string JSON yang gagal akan menampilkan pesan kesalahan yang lebih rinci tentang error dalam memparse. Hal ini sangat berguna ketika mendebug data JSON yang besar.</p>
<pre class="brush: js">JSON.parse('[1, 2, 3, 4,]');
// SyntaxError: JSON.parse: unexpected character at
// line 1 column 13 of the JSON data
</pre>
<h2 id="Lihat_Juga">Lihat Juga</h2>
<ul>
<li>{{jsxref("JSON.stringify()")}}</li>
</ul>
|