Javascript 为什么可以';我是否显示此API的内容?

Javascript 为什么可以';我是否显示此API的内容?,javascript,reactjs,api,axios,Javascript,Reactjs,Api,Axios,我正在尝试显示此API的内容,但我不断收到一个错误,该错误表示: TypeError: this.state.persons.map is not a function 20 | render() { 21 | return ( 22 | <ul> > 23 | {this.state.persons.map(person => <li>{person.result}</li>)}

我正在尝试显示此API的内容,但我不断收到一个错误,该错误表示:

  TypeError: this.state.persons.map is not a function

  20 | render() {
  21 |     return (
  22 |         <ul>
> 23 |             {this.state.persons.map(person => <li>{person.result}</li>)}
  24 |         </ul>
  25 | 
  26 |     );

由于响应是一个json对象,您必须首先使用json.parse()解析它。

您的初始
persons
状态是一个对象,而对象没有
map
方法。将其默认为数组

您还需要访问响应中的数组,地址为
res.data.psc\u leader\u hit\u hr\u dist.queryResults.row

class Data extends Component {
  state = {
     persons: []
   }

   componentDidMount() {
     axios.get('http://lookup-service-prod.mlb.com/json/named.psc_leader_hit_hr_dist.bam?season=2015&game_type=%27D%27&game_type=%27L%27&game_type%27W%27&game_type=%27F%27&min_hip_count=15')
       .then(res => {
         this.setState({ persons: res.data.psc_leader_hit_hr_dist.queryResults.row });
       });
   }
  // ...
}

知道了!谢谢。
class Data extends Component {
  state = {
     persons: []
   }

   componentDidMount() {
     axios.get('http://lookup-service-prod.mlb.com/json/named.psc_leader_hit_hr_dist.bam?season=2015&game_type=%27D%27&game_type=%27L%27&game_type%27W%27&game_type=%27F%27&min_hip_count=15')
       .then(res => {
         this.setState({ persons: res.data.psc_leader_hit_hr_dist.queryResults.row });
       });
   }
  // ...
}