--- title: Body.json() slug: Web/API/Body/json translation_of: Web/API/Body/json ---
{{domxref("Body")}} mixin의 json()
매서드는 {{domxref("Response")}} 스트림을 가져와 스트림이 완료될때까지 읽는다. 이 메서드는 body 텍스트를 {{jsxref("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...
In our fetch json example (run fetch json live), we create a new request using the {{domxref("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); } });
Specification | Status | Comment |
---|---|---|
{{SpecName('Fetch','#dom-body-json','json()')}} | {{Spec2('Fetch')}} |
{{Compat("api.Body.json")}}