如何仅在获取本地响应后将JSON数据打印到控制台?

Shi*_*hew 0 json react-native fetch-api

我用过

const response =await fetch('https://facebook.github.io/react-native/movies.json');
const json= await response.json();
console.log(json.result);
Run Code Online (Sandbox Code Playgroud)

用于打印获取的json数据到控制台,但无法正常工作。如何将获取的数据直接写入控制台。

Ale*_*nha 6

fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
    console.log(responseJson);
})
Run Code Online (Sandbox Code Playgroud)


Rob*_*ker 5

使用同步/等待的答案(您在问题中使用的答案)

const fetchAndLog = async () => {
    const response = await fetch('https://facebook.github.io/react-native/movies.json');
    const json = await response.json();
    // just log ‘json’
    console.log(json);
}

fetchAndLog();
Run Code Online (Sandbox Code Playgroud)