Note that there are some explanatory texts on larger screens.

plurals
  1. POjson array only gets populated with first value
    primarykey
    data
    text
    <p>Hi i have a problem with a local file wich stores some json data</p> <pre><code>{ "tournament": [{ "TeamName": "AS Roma", "TeamPlayer": "Rickard" } { "TeamName": "Inter", "TeamPlayer": "Bobban" }] }​ </code></pre> <p>Then on buttonclick i try to populate an array with this data. But it only takes "AS Roma" and "Rickard" instead of "As Roma" "Rickard" AND "Inter" "Bobban".</p> <pre><code>// Get teams var url = "http://localhost:57608/json/teams.txt"; $("#btnGetTeams").on('click', function() { var allItems = []; $.getJSON(url, function(data) { $.each(data, function(i, team) { allItems.push({ theTeam: team.tournament.TeamName, thePlayer: team.tournament.TeamPlayer }); }); $.each(allItems, function(i, val) { $('#teams').append('&lt;p&gt;' + val.theTeam + val.thePlayer + '&lt;/p&gt;'); }); }); });​ </code></pre> <p>So instead of output AS Roma Rickard Inter Bobban, it only writes AS Roma Rickard. What am i doing wrong?</p> <p><strong>Update Fully working code thanks to Sushanth</strong></p> <pre><code>// Get teams var url = "http://localhost:57608/json/teams.txt"; $("#btnGetTeams").on('click', function () { var allItems = []; $.getJSON(url, function (data) { $.each(data.tournament, function (i) { allItems.push({ theTeam: data["tournament"][i]["TeamName"], thePlayer: data["tournament"][i]["TeamPlayer"], }); }); $.each(allItems, function (i, val) { $('#teams').append('&lt;p&gt;' + val.theTeam + val.thePlayer + '&lt;/p&gt;'); }); }); }); </code></pre> <p>With Json</p> <pre><code>{ "tournament": [ { "TeamName": "AS Roma", "TeamPlayer": "Rickard" }, { "TeamName": "Inter", "TeamPlayer": "Bobban" } ] </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload