Ce que j'essaye d'obtenir, c'est de sélectionner la valeur "url" du "medium-mp4"

Composant

    componentDidMount() {

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

Appel Json
Lien

-1
bdroid 20 nov. 2018 à 09:10

4 réponses

Meilleure réponse

La réponse n'est pas un tableau, c'est un objet, vous ne pouvez donc pas utiliser 0. Essayez plutôt la clé d'objet que vous souhaitez.

{
  videoUrl: responseJson['hd720-mp4'].url
}
1
Rafael Grilli 20 nov. 2018 à 06:15
                let objMusic = JSON.parse('{"hd720-mp4":{"itag":"22","quality":"hd720","type":"video/mp4","url":"https://r5---sn-5uaezn6y.googlevideo.com/videoplayback?mn=sn-5uaezn6y,sn-q4flrnl7&mm=31,26&id=o-AID9zwFrzXB9sFZjRYPFuJyVV_ViYTpwz40-0Po2qR-P&c=WEB&mime=video/mp4&lmt=1537717779316991&ip=2a02:4780:1:3::f&pl=48&itag=22&dur=295.706&mv=u&source=youtube&ms=au,onr&fvip=5&ei=sKPzW-6XIZCPj-8PzruZ-A4&requiressl=yes&sparams=dur,ei,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,expire&mt=1542693264&expire=1542715408&ipbits=0&key=yt6&signature=B59110AC6E15FAE514C4B18679750702C9069E83.B0AC77A3AEBC7EC3CE20D218C67E8AE80E745E8A&ratebypass=yes&signature="},"medium-webm":{"itag":"43","quality":"medium","type":"video/webm","url":"https://r5---sn-5uaezn6y.googlevideo.com/videoplayback?mn=sn-5uaezn6y,sn-q4flrnl7&mm=31,26&id=o-AID9zwFrzXB9sFZjRYPFuJyVV_ViYTpwz40-0Po2qR-P&lmt=1540750584717310&ip=2a02:4780:1:3::f&pl=48&dur=0.000&mv=u&source=youtube&ms=au,onr&ei=sKPzW-6XIZCPj-8PzruZ-A4&sparams=clen,dur,ei,gir,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,expire&mt=1542693264&expire=1542715408&txp=5511222&key=yt6&gir=yes&ipbits=0&c=WEB&mime=video/webm&itag=43&fvip=5&ratebypass=yes&requiressl=yes&clen=30067021&signature=238FF11CC09F405CFD24423203F813EDFB5D66C2.3003162315BC1FF6A5D0C3B19BB03F9E1016E780&signature="},"medium-mp4":{"itag":"18","quality":"medium","type":"video/mp4","url":"https://r5---sn-5uaezn6y.googlevideo.com/videoplayback?mn=sn-5uaezn6y,sn-q4flrnl7&mm=31,26&id=o-AID9zwFrzXB9sFZjRYPFuJyVV_ViYTpwz40-0Po2qR-P&lmt=1537716294977165&ip=2a02:4780:1:3::f&pl=48&dur=295.706&mv=u&source=youtube&ms=au,onr&ei=sKPzW-6XIZCPj-8PzruZ-A4&sparams=clen,dur,ei,gir,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,expire&mt=1542693264&expire=1542715408&key=yt6&gir=yes&ipbits=0&c=WEB&mime=video/mp4&itag=18&fvip=5&ratebypass=yes&requiressl=yes&clen=23503941&signature=DA800A9574CBF4EA8666390C6F98562A70EBE3EE.9C3BD58181B597C780E5264498B0919D2B0B8453&signature="},"small-3gpp":{"itag":"17","quality":"small","type":"video/3gpp","url":"https://r5---sn-5uaezn6y.googlevideo.com/videoplayback?mn=sn-5uaezn6y,sn-q4flrnl7&mm=31,26&txp=5531232&id=o-AID9zwFrzXB9sFZjRYPFuJyVV_ViYTpwz40-0Po2qR-P&c=WEB&sparams=clen,dur,ei,gir,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,requiressl,source,expire&mime=video/3gpp&lmt=1540636712468197&ip=2a02:4780:1:3::f&pl=48&itag=17&dur=295.729&mv=u&source=youtube&ms=au,onr&fvip=5&ei=sKPzW-6XIZCPj-8PzruZ-A4&requiressl=yes&clen=2800636&mt=1542693264&expire=1542715408&ipbits=0&key=yt6&signature=4E618E1E6289DAC12A897896C9D2DEDA304DB55D.2240D0C7E9911C62537A948D6795EF7E171AA7CC&gir=yes&signature="}}')
                if (objMusic['medium-mp4']) {
                    let mp4 = objMusic['medium-mp4']
                    let url = mp4.url
                    alert(JSON.stringify(url))
                }

Vous pouvez utiliser ci-dessus pour obtenir la valeur d'URL.

2
Nirmalsinh 20 nov. 2018 à 06:27
responseJson['hd720-mp4']['url']
0
Randy Rebucas 20 nov. 2018 à 06:22

Essayez cet extrait de code

videoUrl: responseJson['medium-mp4'].url
1
Advait 20 nov. 2018 à 06:30