How to print JSON data to console only after fetch in react native?

时间:2018-01-23 19:17:14

标签: json react-native fetch-api

i have used

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

for print fetched json data to console but its not working. how to write fetched data directly to console.

4 个答案:

答案 0 :(得分:4)

使用sync / await(您在问题中使用)的答案

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我使用这种方式它工作正常。

fetch('https://facebook.github.io/react-native/movies.json')  
 .then((response) => response.text())
 .then((responseText) => {
     console.log(JSON.parse(responseText));
 })
 .catch((error) => {
     console.log("reset client error-------",error);
});

以下用于特定方法请求。标题和正文用于向服务器发送数据。为此,我们可以请求获取函数的类型和方法。

      fetch(url, {
            method: 'POST', 
            timeout:10000,
            headers: headers,
            body: JSON.stringify(params) 
        })  
        .then((response) => response.text())
        .then((responseText) => {
             console.log(JSON.parse(responseText));
        })
        .catch((error) => {
             console.log("reset client error-------",error);
        });
    });

答案 3 :(得分:1)

我认为这是正确的写作方式:

int isdominant(int *arr, size_t cols, size_t rows, size_t col, size_t row)
{
    int *rowend = arr + (row + 1) * cols - 1;
    int *pos = arr + row * cols + col;
    while(rowend > pos)
        if(*pos <= *rowend--) return 0;
    return 1;
}

#define COLS 10
#define ROWS 7

int main()
{
    int arr[ROWS][COLS];
    int *ptr = &arr[0][0];
    unsigned dominants = 0;


    for(size_t index = 0; index < ROWS * COLS; index++) *ptr++ = rand() % 50;

    for(size_t row = 0; row < ROWS; row++)
    {
        for(size_t col = 0; col < COLS; col++)
        {
            int dom = 0;
            if(col != COLS - 1) dom = isdominant(arr[0], COLS, ROWS, col, row);
            printf("%s%d%s\t", dom ? "(" : "", arr[row][col], dom ? ")" : "");
            dominants += dom;
        }
        printf("\n");
    }
    printf("Dominant numbers: %u\n", dominants);
}