Body
mixin의 json()
매서드는 Response
스트림을 가져와 스트림이 완료될때까지 읽는다. 이 메서드는 body 텍스트를 JSON
으로 바꾸는 결과로 해결되는 promise를 반환한다.
구문
response.json().then(function(data) {
// do something with your data
});
매개변수
없음.
반환값
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...
Example
In our fetch json example (run fetch json live), we create a new request using the Request.Request
constructor, then use it to fetch a .json
file. When the fetch is successful, we read and parse the data using json()
, then read values out of the resulting objects as you'd expect and insert them into list items to display our product data.
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);
}
});
Specifications
Specification | Status | Comment |
---|---|---|
Fetch The definition of 'json()' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.