我使用javascript来提取数据,但不知道该怎么做
"players": [{
"name": "Kyle Rudolph",
"jersey": "82",
"last_name": "Rudolph",
"first_name": "Kyle",
"abbr_name": "K.Rudolph",
"preferred_name": "Kyle",
"birth_date": "1989-11-09",
"weight": 265.0,
"height": 78,
"status": "A01",
"id": "1059e9dc-97df-4643-9116-883a0573d8b1",
"position": "TE",
"birth_place": "Cincinnati, OH, USA",
"high_school": "Elder (OH)",
"college": "Notre Dame",
"college_conf": "Independent",
"rookie_year": 2011,
"draft": {
"year": 2011,
"round": 2,
"number": 43,
"team": {
"name": "Vikings",
"market": "Minnesota",
"alias": "MIN",
"id": "33405046-04ee-4058-a950-d606f8c30852"
}
},
答案 0 :(得分:1)
约翰,你不必使用相同的名字。一旦您的PHP从API接收数据,将其解析为您想要的任何名称,忽略您不需要的值等等...例如,假设API为您提供名字和姓氏,但在您的数据库中只关心全名。另一个例子,如果API给你的球员体重(磅),但你需要它以千克为单位:
$api_result = file_get_contents($url);
$api_data = json_decode($api_result);
$name = "$api_data->last, $api_data->first"
$weight = $api_data->weight * 0.454; //convert pounds to kg for storage
现在,您可以根据需要在数据库中存储$name
和$weight
。当您的网站从后端提取数据时,API生成的数据形状并不重要,因为您将其存储在对您的应用程序最有帮助的形状中