My .json
file is as follows:
{"Team1":
{
"Player1": {"minutes":"11:35", "tsr":"25.0"},
"Player2":{"minutes":"24:44", "tsr":"53.8"},
"Player3":{"minutes":"14:22", "tsr":"35.7"}
},
"Team2":
{
"PlayerX":{"minutes":"27:06", "tsr":"12.5"},
"PlayerY":{"minutes":"04:54", "tsr":"27.8"},
"PlayerZ":{"minutes":"03:21", "tsr":"33.7"}
}
}
I want to echo a table, as follows:
<table>
<tr><td colspan='3'>Team1</td></tr>
<tr>
<td>Player1</td>
<td>11:35</td>
<td>25.0</td>
</tr>
<tr>
<td>Player2</td>
<td>24:44</td>
<td>53.8</td>
</tr>
<tr>
<td>Player3</td>
<td>14:22</td>
<td>35.7</td>
</tr>
<tr><td colspan='3'>Team2</td></tr>
<tr>
<td>PlayerX</td>
<td>27:06</td>
<td>12.5</td>
</tr>
<tr>
<td>PlayerY</td>
<td>04:54</td>
<td>27.8</td>
</tr>
<tr>
<td>PlayerZ</td>
<td>03:21</td>
<td>33.7</td>
</tr>
</table>
The number of players in each team is not fixed: it can be 3 or other number.
I am using jquery to refresh the .json
file. This is the current (and bad) jquery code. It should be something like this:
var pdatafile = "json/pdata.json";
$.getJSON(pdatafile, function (pjson) {
for (i = 0; i < pjson.Team1.length; ++i) {
// Code
}
for (i = 0; i < pjson.Team2.length; ++i) {
// Code
}
});
Could anyone tell me how to make this jquery code works and how to print the table in jquery?