blob: c3c0fa14ccba649543583a5f4c008ac2ed8e3281 (
plain)
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
|
---
title: Body.json()
slug: orphaned/Web/API/Body/json
translation_of: Web/API/Body/json
original_slug: Web/API/Body/json
---
<div>{{APIRef("Fetch")}}</div>
<p>{{domxref("Body")}} mixin의 <strong><code>json()</code></strong> 매서드는 {{domxref("Response")}} 스트림을 가져와 스트림이 완료될때까지 읽는다. 이 메서드는 body 텍스트를 {{jsxref("JSON")}}으로 바꾸는 결과로 해결되는 promise를 반환한다.</p>
<h2 id="구문">구문</h2>
<pre class="brush: js">response.json().then(function(data) {
// do something with your data
});</pre>
<h3 id="매개변수">매개변수</h3>
<p>없음.</p>
<h3 id="반환값">반환값</h3>
<p>A promise that resolves with the result of parsing the body text as JSON. This could be anything that can be represented by JSON — an object, an array, a string, a number...</p>
<h2 id="Example">Example</h2>
<p>In our <a href="https://github.com/mdn/fetch-examples/tree/master/fetch-json">fetch json example</a> (run <a href="http://mdn.github.io/fetch-examples/fetch-json/">fetch json live</a>), we create a new request using the {{domxref("Request.Request")}} constructor, then use it to fetch a <code>.json</code> file. When the fetch is successful, we read and parse the data using <code>json()</code>, then read values out of the resulting objects as you'd expect and insert them into list items to display our product data.</p>
<pre class="brush: js">var myList = document.querySelector('ul');
var myRequest = new Request('products.json');
fetch(myRequest)
.then(function(response) { return response.json(); })
.then(function(data) {
for (var i = 0; i < data.products.length; i++) {
var listItem = document.createElement('li');
listItem.innerHTML = '<strong>' + data.products[i].Name + '</strong> can be found in ' +
data.products[i].Location +
'. Cost: <strong>£' + data.products[i].Price + '</strong>';
myList.appendChild(listItem);
}
});</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Fetch','#dom-body-json','json()')}}</td>
<td>{{Spec2('Fetch')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Body.json")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
<li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
<li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>
|