【问题标题】:How i can select a specific value from json in react native?我如何在本机反应中从 json 中选择特定值?
【发布时间】:2019-04-21 21:05:53
【问题描述】:

我想要得到的是选择“质量”的“url”值为 hd720

组件

组件DidMount() { return fetch('https://you-link.herokuapp.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY') .then((响应) => response.json()) .then((responseJson) => { 这个.setState({ isLoading:假, 数据源:responseJson, videoUrl:responseJson[0].url }, 功能() { }); }) .catch((错误) => { 控制台.错误(错误); }); }

Json 调用
Link

【问题讨论】:

  • 我看不出您的请求有任何本质上的错误。错误是什么?
  • 你可以使用findresponseJson.find(obj => obj.quality === "hd720").url
  • 我已经尝试了代码但它不起作用:“responseJson.find 不是函数”
  • 试试下面的代码块
  • @bdroid,如果responseJson.find不存在,那么你需要在之前调试响应。 .then(response => { console.log(response); return response.json(); })

标签: javascript reactjs react-native


【解决方案1】:

您只需搜索符合条件的 JSON 数组 (Using find) 在这种情况下“质量”===“hd720”

componentDidMount() {
    return fetch('https://you-link.herokuapp.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY')
        .then((response) => response.json())
        .then((responseJson) => {
            let url = responseJson.find(obj => obj.quality === "hd720").url
            this.setState({
                isLoading: false,
                dataSource: responseJson,
                videoUrl: url
            }, function() {});
        })
        .catch((error) => {
            console.error(error);
        });
}

【讨论】:

  • 我的荣幸。编码愉快。
猜你喜欢
  • 2019-12-10
  • 1970-01-01
  • 2020-11-17
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2022-11-09
  • 1970-01-01
相关资源
最近更新 更多