사용자가 Spotify API에서 아티스트, 앨범 등을 검색 할 수있는 앱이 있습니다. 요청을하고 응답을 얻을 수 있지만 값을 가져 오는 데 어려움이 있습니다. 표시 할 응답.React로 JSON에서 배열 값 가져 오기
App.jsx
class App extends Component{
constructor(props){
super(props);
this.state = {
searchTerm: '',
userData: []
}
}
// Get data from spotify
getUserData(){
$.ajax({
url: 'https://api.spotify.com/v1/search?q='+this.state.searchTerm+'&type=playlist',
dataType: 'json',
cache: false,
success: function(data){
console.log(data.playlists.items);
this.setState({userData: data.playlists.items});
}.bind(this),
error: function(xhr, status, error){
this.setState({searchTerm: null});
alert(error);
}.bind(this)
});
}
handleFormSubmit(searchTerm){
this.setState({searchTerm: searchTerm}, function(){
this.getUserData();
});
}
componentDidMount(){
this.getUserData();
}
render(){
return(
<div>
<Search onFormSubmit = {this.handleFormSubmit.bind(this)} />
<Songs {...this.state}/>
</div>
)
}
}
export default App
Songs.jsx
class Songs extends Component{
render(){
return(
<div className="panel panel-default panel-order">
<div className="panel-body">
<div className="row">
<div className="col-md-1"><img src={this.props.userData.images} /></div>
<div className="col-md-11">
<div className="row">
<div className="col-md-12">
<span><strong>Track Name: {this.props.userData.name}</strong></span>
<br /> Tracks:{this.props.userData.tracks}
</div>
<div className="col-md-12">
Type: <p>{this.props.userData.type}</p>
</div>
</div>
</div>
</div>
</div>
</div>
)
}
}
export default Songs
이것은 샘플 응답의 스크린 샷이다. , url
을 images
배열 등에서 가져올 수는 있지만 루프를 통과 할 수는 없습니다.
당신이 이미지 배열을 반복 할을하고 얻을 URL? 이미지 배열의 객체에는 내가 생각하는 이름이 들어 있지 않습니다. –
이미지 URL을 가져올 수 있고 이름 (이미지 배열과 같은 수준에 있음)을 추가하고 싶습니다. – Matt
@MayankShukla -이 경우에는 "Dragons Best Of"를 나타내는 이름을 인쇄 할 수 있기를 원합니다. – Matt