2012-07-04

Simple example for parseJSON function

Following is a simple sample app which parse a JSON string to a jQuery object and access a value in the JSON string.

<html>
<head>
    <title></title>
    <script src="
http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var str = "[{\"CityName\":\"Colombo\",\"CityID\":1,\"ZipCodeID\":1,\"ZipCode\":1098},
                       {\"CityName\":\"Kandy\",\"CityID\":2,\"ZipCodeID\":2,\"ZipCode\":1675}]";
        var test = jQuery.parseJSON(str);
        for (var i = 0; i < test.length; i++)
            alert(test[i].CityName); //Here, this will alert the City name in the JSON string
    </script>
</head>
<body>
</body>
</html>

No comments: