I have a PHP file running a number of queries, storing the sets of results in multidimensional associative array and encoding as JSON like so:
{"1":{"name":"Bob","score":18},"3":{"name":"Robert","score":12},"4":{"name":"Terry","score":22}...}
So the array contains a set of arrays with ID numbers as keys, each containing the elements ‘name’ and ‘age’.
I am serving some HTML like this:
<div id="game1">high scorer: Bob<br />score: 18</div>
<div id="game3">high scorer: Robert<br />score: 12</div>
<div id="game4">high scorer: Terry<br />score: 22</div>
And I want the info in these divs to update according to the JSON string as served by the PHP, so if the scores have been beaten to update with the new information.
I have tried using .get() and .load() but not really sure what to do to extract the elements from the JSON and disperse them to the divs.
How about using getJSON() method of jQuery?
http://api.jquery.com/jQuery.getJSON/
Edit: You would need to tweak the implementation slightly so that it matches your jSon data structure – what I’ve added above is based on a generic name/value pair structure within the jSon data.