MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

Body.json()

这篇翻译不完整。请帮忙从英语翻译这篇文章

Body  mixin 的 json() 方法使用一个 Response 流,并将其读取完成。它返回一个 promise ,解析结果是将文本体解析为 JSON
 

Syntax

response.json()
.then((data) => {
    // do something with your data
});


fetch(`https://cdn.xgqfrms.xyz/json/badges.json`)
.then((response) => response.json())
.then((data) => {
    console.log(`data = \n`, data)
});

Parameters

None.

Returns

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
json()
Living Standard  

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42 [1]
 
(Yes) 39 (39) [2] 未实现

29 [3]

未实现
Feature Android Edge Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 (Yes) 未实现 未实现 未实现 未实现 未实现 未实现

[1] Behind a preference in version 41.

[2] Behind a preference starting with version 34.

[3] Behind a preference in version 28.

See also

文档标签和贡献者

标签: 
 此页面的贡献者: xgqfrms-GitHub
 最后编辑者: xgqfrms-GitHub,