首页 文章

我怎样才能从json中选择一个特定的值来反应原生?

提问于
浏览
3

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

Component

componentDidMount() {

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

Json Call
Link

1 回答

  • 4

    您只需要搜索具有匹配条件的JSON数组(Using find),在这种情况下"quality" === "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);
            });
    }
    

相关问题