【问题标题】:Accessing Objects Value from json response react native从json响应访问对象值反应本机
【发布时间】:2018-07-24 12:51:42
【问题描述】:

以下是 JSON 响应:

{
    "status": "passedd",
    "reasons": {
        "welcome": {
            12: "helo",
            14: "hi",
            15: "fine",
            14: "Not interested"
        },
        "speakin": {
            16: "Phone",
            19: "Msg",
            21: "Whatssup"
        },
        "Byee": {
            17: "Gud bye",
            25: "Need help",
            26: "byeee",
            27: "Help"
        }
    }
}

我需要访问对象的值,例如:Helo。由于我是反应原生的新手,我不清楚访问对象值。

使用 Objects.keys(reasons) 我可以得到欢迎,说话,再见。如何访问其下的键和值?请帮帮我。

【问题讨论】:

  • 我建议阅读有关对象的基本 JavaScript 教程;如果你不能做到这一点,那么有效地使用 React 将非常困难。
  • “我可以受到欢迎,说话,再见”...好的,访问下面的有什么问题?你用了什么代码?什么地方出了错?顺便说一句,这只是基本的 JS,不需要 React。也许你应该读到这个:stackoverflow.com/questions/16908476/…

标签: arrays json object react-native javascript-objects


【解决方案1】:

试试这个

 fetch(.....)
        .then(res => res.json())
        .then((responseJson) => {
            welcome = responseJson.reasons.welcome
            helo = welcome.12
        })
        .catch((error) => {
          .....
        });
}

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多