You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中console.log无法获取JSON属性值仅返回数组问题排查

问题原因及解决方案

问题根源

你通过fetch获取到的json是包含多个对象的数组,而非单个对象。数组本身不存在address属性,直接执行console.log(json.address)自然会返回undefined。

正确访问方式

要获取address,需先定位数组内的具体对象,再访问对应属性:

1. 访问数组第一个对象的address

console.log(json[0].address);
// 输出:Parque das Nações, Parnamirim, , Rio Grande do Norte, BR

2. 遍历数组获取所有对象的address

如果需要处理所有对象的地址,可用循环遍历:

json.forEach(item => {
  console.log(item.address);
});
// 依次输出:
// Parque das Nações, Parnamirim, , Rio Grande do Norte, BR
// Rio Grande do Norte, BR

3. 调整你的fetch代码示例

在现有代码中处理地址可参考:

async function buscarPosicoes() {
    fetch("https://xxx.xxx")
      .then((resposta) => resposta.json())
      .then((json) => {
        setPositions(json);
        // 获取第一个对象的地址
        console.log(json[0].address);
        // 或遍历所有地址
        json.forEach(pos => console.log(pos.address));
      })
      .catch((error) => console.error(error));
}

内容的提问来源于stack exchange,提问作者Junior Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 17:10:09