JSON javascript显示值未定义

JSON javascript显示值未定义,javascript,json,Javascript,Json,我返回了一个JSON对象,如下所示 [ { "competition": { "name": "Premier League", }, "nextState": 1, "team_id": 1 }, { "competition": { "name": "Premier League", }, "nextState": 1,

我返回了一个JSON对象,如下所示

[
    {
        "competition": {
            "name": "Premier League",
        },
        "nextState": 1,
        "team_id": 1
}, {
        "competition": {
            "name": "Premier League",
        },
        "nextState": 1,
        "team_id": 2
}
]
这是一个精简版的JSON,我试图访问团队id

result = JSON.stringify(result, null, 4);
    console.log(result);

    $('#test').append(result);

    alert(result[0].team_id);
所有我似乎得到的是未定义的,我不是正确地访问这个吗


非常感谢

您已将对象
结果
字符串化。因此,您肯定不能再访问这些属性了

为字符串化结果使用第二个变量:

var result_stringified = JSON.stringify(result, null, 4);
console.log(result_stringified);
$('#test').append(result_stringified);
alert(result[0].team_id);
试试这个:

var data = [{
      "competition": {
        "name": "Premier League",
      },
      "nextState": 1,
      "team_id": 1
    }, {
      "competition": {
        "name": "Premier League",
      },
      "nextState": 1,
      "team_id": 2
    }]



    $.each(data, function(k, v) {
      alert(this.team_id);
    })

之所以看到未定义的,是因为您已经将JSON对象字符串化为字符串。那么一根绳子数组,因此执行字符串[0]将给出未定义的,显然执行
。在未定义的数组上,team_id
仍将是
未定义的

这里有一个javascript
对象的
数组

要访问对象的特定
团队id
属性,首先必须从数组中访问该
对象
,然后使用
团队id
符号或
对象上的
[“团队id”]
语法

示例:

var项=[
{
“竞争”:{
“名称”:“英超联赛”,
},
“下一州”:1,
“团队id”:1
},
{
“竞争”:{
“名称”:“英超联赛”,
},
“下一州”:1,
“团队id”:2
}
]

items.forEach(item=>{console.log(“item_id=“+item.team_id+”,competition=“+item.competition.name”);})
“[”。团队id始终未定义。不要将其字符串化您正在将数据结构(JSON)转换为线性字符链(字符串)。难怪你不能再访问数据结构了。如今,每个人都会出于某种原因,在发送JSON之前,在接收JSON之后,一直对JSON进行字符串化,这是一个我无法解释的趋势。谢谢大家,所有的答案都很有帮助。接受的答案是第一个,不过感谢大家。