Json with Jquery
Today, I am think about ornagai api. I should export with JSON because json is easy to read data with javascript library (mootools,jquery,etc..) and also with PHP. So, I tring with Jquery to read json. Oh! it’s so easy and so nice for me.
This is a data.json
{
“data”:
[{
"word":"A",
"state":"art",
"def":" the first letter of the English alphabet"
},
{
"word":"Art",
"state":"n",
"def":"the quality, production, expression, or realm, according to aesthetic principles, of what is beautiful, appealing, or of more than ordinary significance"
}
,
{
"word":"Saturn",
"state":"n",
"def":"an ancient Roman god of agriculture, the consort of Ops, believed to have ruled the earth during an age of happiness and virtue, identified with the Greek god Cronus."
}]}
This is my jquery code
[sourcecode lang='js']
$(function() {
$.getJSON(“data.json”, function(json) {
alert(json.data[1].def);
$.each(json.data,function(i,mydata){
$(“#result”).append(mydata.word+”<br>”);
});
});
});
[/sourcecode]
Complete HTML code is
[sourcecode lang='xhtml']
<html>
<head>
<script src=”jquery-1.3.2.min.js”></script>
<script>
$(function() {
$.getJSON(“data.json”, function(json) {
alert(json.data[1].def);
$.each(json.data,function(i,mydata){
$(“#result”).append(mydata.word+”<br>”);
});
});
});
</script>
</head>
<body>
<div id=”result”>
</div>
</body>
</html>
[/sourcecode]