2016-04-15 254 views
0

我想讀取一個jQuery文件與嵌套元素使用jquery和我可以調用並返回第一級,但是當我調用「結果」它返回的結果,但5「未定義」與它也閱讀嵌套json與jQuery返回undefined

這裏是我的JSON:

{ 
"count": 380, 
"fixtures": [ 
{ 
    "status": "FINISHED", 
    "homeTeamName": "Manchester United FC", 
    "awayTeamId": 73, 
    "matchday": 1, 
    "homeTeamId": 66, 
    "result": { 
    "goalsAwayTeam": 0, 
    "goalsHomeTeam": 1 
    }, 
    "date": "2015-08-08T11:45:00Z", 
    "soccerseasonId": 398, 
    "awayTeamName": "Tottenham Hotspur FC", 
    "id": 147075 
}, 
{ 
    "status": "FINISHED", 
    "homeTeamName": "Everton FC", 
    "awayTeamId": 346, 
    "matchday": 1, 
    "homeTeamId": 62, 
    "result": { 
    "goalsAwayTeam": 2, 
    "goalsHomeTeam": 2 
    }, 
    "date": "2015-08-08T14:00:00Z", 
    "soccerseasonId": 398, 
    "awayTeamName": "Watford FC", 
    "id": 147073 
} 

這是我的HTML和JQuery:

<!DOCTYPE html> 
<html> 
<head> 
    <title> </title> 
    <style> 
th, td { 
border-bottom: 1px solid #ddd; 
} 
tr:nth-child(even){background-color: #f2f2f2} 

</style> 
</head> 
<body> 
     <table id="data"> 
      <tr> 
       <td>status</td> 
       <td>team 2</td> 
       <td>team 1 </td> 
       <td>score team 2</td> 
       <td>score team 1</td> 
      </tr> 


     </table> 

    <script  src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
    <script>/*global $*/ /*global result*/ 
    $.ajax({ 
     url: 'data.json', 
     dataType: 'json', 
     type: 'get', 
     cache: false, 
     success: function(data) { 
      $(data.fixtures).each(function(index, value) { 

       $("#data").append("<tr><td>"+value.status +"</td>"+" <td>"+value.homeTeamName+"</td>"+"<td>"+value.awayTeamName+"</td>"); 
       $.each(value, function(result , v) { 

      $("#data").append("<tr><td>"+v.goalsHomeTeam +"</td>"+"<td>"+v.goalsAwayTeam+"</td></tr>"); 
       }); 
      }); 


      } 


    }); 
    </script> 
</body> 
</html> 

這裏是輸出:

FINISHED Manchester United FC Tottenham Hotspur FC 
undefined undefined 
undefined undefined 
undefined undefined 
undefined undefined 
undefined undefined 
1 0 

我希望我解釋得不錯,感謝任何幫助。

回答

1

JQuery each

看起來像你不需要把數組作爲第一個參數

$.each(data.fixtures, function(index, value) { 

      $("#data").append("<tr><td>"+value.status +"</td>"+" <td>"+value.homeTeamName+"</td>"+"<td>"+value.awayTeamName+"</td>"); 

      $("#data").append("<tr><td></td><td>"+value.result.goalsHomeTeam +"</td>"+"<td>"+value.result.goalsAwayTeam+"</td></tr>"); 

     }); 

,而不是選擇有它

+0

仍在生產的不確定 –

+0

相同的列表是有可能如果不是那麼第二個是不需要的,你可以使用append line,但用'value.result'代替'v' –

+0

, –