What you want to do is first parse the JSON file, then use your function for the array.
You can get the JSON from a file by using JQuery like this:
http://api.jquery.com/jquery.getjson/
$.getJSON("js/myjson.json", function(json){
// json is an object which contains the array called "myjson"
// now you can pass that array into your function like this:
myjson(json.myjson);
})
If you don't want to use JQuery look how it's done without here:
How can I open a JSON file in JavaScript without jQuery?
Edit: Okay lets do it as a whole.
HTML:
<div id="id01"></div>
Javascript:
function myjson(arr) {
var out = "";
var i;
for(i = 0; i<arr.length; i++) {
out += '<a href="' + arr[i].id + '">' + arr[i].name + '</a><br>';
}
document.getElementById("id01").innerHTML = out;
}
function loadJSON(path, success, error)
{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
if (success)
success(JSON.parse(xhr.responseText));
} else {
if (error)
error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
}
loadJSON('js/myjson.json', function(data){ myjson(data.myjson);}, function(xhr) {console.error(xhr); });
You can either put your Javascript inside your HTML with a script tag like
<script language="javascript" type="text/javascript">//Javascript goes here</script>
or put it into a seperate file and just link it in your HTML like this
<script language="javascript" type="text/javascript" src="js/myscript.js"></script>