Monday, October 29, 2012

Parse JSON with JQuery

JSON file - json.php

[sourcecode language="php"]
<?php
$json = '{ "students":[

{
"id":"001",
"name" :{"first" : "rahul", "last" : "mani"},
"age" : 20,
"city" : "hydrabad"
},
{
"id":"002",
"name" :{"first" : "deepu", "last" : "patel"},
"age" : 18,
"city" : "delhi"
}
]}';
echo $json;
?>
[/sourcecode]

 


[sourcecode language="html"]
<html>
<head>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url = "json.php";
$.getJSON(url,function(json){
$.each(json.students,function(i,student){
$("#response").append(
student.id + ' '+ student.name.first + ' ' + student.age + ' ' + student.city + '<br>'
);
});
});
});
</script>
</head>
<body>
<div id="response"></div>
</body>
</html>
[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...