I have json file mydata.json
, and in this file is some json-encoded data.
I want obtain this data in file index.html
and process this data in JavaScript. But a don't know how to connect.json file in .html file?
Tell me please. Here is my json file:
{
"items": [
{
"movieID": "65086",
"title": "The Woman in Black",
"poster": "/kArMj2qsOnpxBCpSa3RQ0XemUiX.jpg"
},
{
"movieID": "76726",
"title": "Chronicle",
"poster": "/853mMoSc5d6CH8uAV9Yq0iHfjor.jpg"
}
]
}
Thinking that I am getting json file from server, how to use that file in my html, so that I can display the data in tables in html page. I am using JavaScript to parse the json file. I am new to this field. Help out please.
This question is related to
javascript
jquery
html
json
get
You can use JavaScript like... Just give the proper path of your json file...
<!doctype html>
<html>
<head>
<script type="text/javascript" src="abc.json"></script>
<script type="text/javascript" >
function load() {
var mydata = JSON.parse(data);
alert(mydata.length);
var div = document.getElementById('data');
for(var i = 0;i < mydata.length; i++)
{
div.innerHTML = div.innerHTML + "<p class='inner' id="+i+">"+ mydata[i].name +"</p>" + "<br>";
}
}
</script>
</head>
<body onload="load()">
<div id="data">
</div>
</body>
</html>
Simply getting the data and appending it to a div... Initially printing the length in alert.
Here is my Json file: abc.json
data = '[{"name" : "Riyaz"},{"name" : "Javed"},{"name" : "Arun"},{"name" : "Sunil"},{"name" : "Rahul"},{"name" : "Anita"}]';
use jQuery's $.getJSON
$.getJSON('mydata.json', function(data) {
//do stuff with your data here
});
Source: Stackoverflow.com