无法从json对象获取数据

时间:2019-04-08 11:53:02

标签: javascript json

我正在尝试console.log对象的“名称”。每当我尝试此操作时,它都会返回“未定义”。我该如何解决?

    fetch('http://localhost:9000/quiz/code', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: code
        })
    }).then(res => res.json())
        .then(data => {
            data = JSON.stringify(data);
            console.log("data: " + data);
            console.log("name: " + data.name);
        })

这是我第一个console.log的输出:

data: [{"_id":"5cab356c8b35014074855ada","name":"fsgsdfgd","vragen":{"vragen":[{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"},{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"}]}}]

这是第二个console.log的输出:

name: undefined

5 个答案:

答案 0 :(得分:4)

您正在对响应数据使用stringify(),然后将其视为对象。那行不通。只需删除data = JSON.stringify(data);,因为您已经拥有有效的JSON(以数组的形式)。您可以简单地遍历数组中的对象并获取每个对象的名称:

fetch('http://localhost:9000/quiz/code', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        name: code
    })
}).then(res => res.json())
    .then(data => {
        console.log("data: ", data);
        data.forEach(function(obj)  {
            console.log("name: " + obj.name);
        });
        console.log("name: " + data[0].name);
    })

答案 1 :(得分:2)

删除字符串化并使用数据[0] .name

data = [{"_id":"5cab356c8b35014074855ada","name":"fsgsdfgd","vragen":{"vragen":[{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"},{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"}]}}]

console.log(data[0].name)

答案 2 :(得分:2)

我删除了JSON.stringify(data)并将data [0] .name添加到了console.log。这样就解决了问题。我的新代码如下:

fetch('http://localhost:9000/quiz/code', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: code
        })
    }).then(res => res.json())
        .then(data => {
            data = data;
            console.log("name: " + data[0].name);
        })

答案 3 :(得分:-2)

数据:[{“是数组

在获取属性之前,您应该访问array元素  名称:

按索引访问的示例:

console.log("name: " + data[0].name);

答案 4 :(得分:-3)

您位于http://localhost:9000/quiz/code的服务器正在返回一个数组。

您应该将服务器返回或客户端代码更改为:

console.log("name: " + data[0].name);

您的最终代码应类似于:

 fetch('http://localhost:9000/quiz/code', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: code
        })
    }).then(res => res.json())
        .then(data => {
            data = JSON.stringify(data);
            console.log("data: " + data);
            console.log("name: " + data[0].name);
        })