I want to deserialize the JSON string below, using jQuery each function, but the item is undefined.
there is the code below, but, in case I am using asp.net 2.0 + web service and fill the DataTable and pass to System.Web.Script.Serialization.JavaScriptSerializer class and return these JSON string.
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var $strJson = '[';
$strJson += '{"Code":"a","Name":"Sam","Country":"US"},';
$strJson += '{"Code":"b","Name":"John","Country":"CN"},';
$strJson += '{"Code":"c","Name":"Mary","Country":"TW"}';
$strJson += ']';
$.each($strJson, function (key, item) {
alert(item);
if (key == 0) return false; //Prevent infinity loop
});
});
</script>
</head>
<body>
</body>
</html>
Just use the built-in JavaScript eval method:
Incidentally, you don't need to use the $ sign for variables in JavaScript.
Have you tried using ASP.NET's Sys.Serialization.JavaScriptSerializer's deserialize method?
Alternatively, there is json_parse
Working Example here. Here's the code