CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Parsing or Reading a Simple JSON string using jQuery in ASP.Net
Submitted By Bala Murugan
On 4/7/2010 9:24:04 AM
Tags: CodeDigest,jquery,json  

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>


 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..