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

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

语法

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)
});

参数

没有。

返回值

返回一个被解析为JSON格式的promise对象,这可以是任何可以由JSON表示的东西 - 一个object,一个array,一个string,一个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

文档标签和贡献者

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