我如何从json中选择特定值?

时间:2018-11-20 06:10:57

标签: javascript reactjs react-native

我要获取的是选择“ medium-mp4”的“ url”值

组件

    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);
         });
     }

Json Call
Link

4 个答案:

答案 0 :(得分:2)

                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))
                }

您可以使用上面的方法获取url值。

答案 1 :(得分:1)

响应不是数组,而是对象,因此不能使用0。请改用所需的对象键。

{
  videoUrl: responseJson['hd720-mp4'].url
}

答案 2 :(得分:1)

尝试此代码段

videoUrl: responseJson['medium-mp4'].url

答案 3 :(得分:0)

responseJson['hd720-mp4']['url']