Parsing or Reading a Simple JSON string using jQuery
Sometimes, we will require reading or parsing a simple json string returned from the server and populate it to a HTML element. The below code snippet will help us to do that.
<script src="../_scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script language=" text/javascript"> $(document).ready(function() { $("#Button1").click(function() {
var jsonp = '[{"Dept":"CSE","ID":"1"},{"Dept":"IT","ID":"2"}]';
var obj = $.parseJSON(jsonp); $.each(obj, function() { alert(this['Dept']); //Do your operations }); }); }); </script>
If you are using jquery library less than 1.4.x version, the below snippet can be used.
<script language=" text/javascript"> $(document).ready(function() { $("#Button1").click(function() { var jsonp = '[{"Dept":"CSE","ID":"1"},{"Dept":"IT","ID":"2"}]'; $.each(eval(jsonp), function() { alert(this['Dept']); //Do your operations }); }); }); </script>
|