ASP.NET + jQuery,如何反序列化JSON? [英] ASP.NET+jQuery, how to deSerialize JSON?

查看:100
本文介绍了ASP.NET + jQuery,如何反序列化JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery每个函数反序列化下面的JSON字符串, 但该项目未定义.

I want to deserialize the JSON string below, using jQuery each function, but the item is undefined.

下面是代码,但是,如果我使用asp.net 2.0 + Web服务并填充DataTable并传递给System.Web.Script.Serialization.JavaScriptSerializer类并返回这些JSON字符串.

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>

推荐答案

您是否尝试过使用ASP.NET的Sys.Serialization.JavaScriptSerializer的

Have you tried using ASP.NET's Sys.Serialization.JavaScriptSerializer's deserialize method?

var result = Sys.Serialization.JavaScriptSerializer.deserialize($strJson);

或者,还有 json_parse

var result = json_parse($strJson, [reviver])

此方法解析JSON文本以生成对象或数组. 它可以引发SyntaxError异常.

This method parses a JSON text to produce an object or array. It can throw a SyntaxError exception.

可选的reviver参数是一个可以 过滤并转换结果.它接收每个键和值, 并且使用其返回值代替原始值. 如果返回所收到的内容,则结构不是 修改的.如果返回undefined,则删除该成员.

The optional reviver parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted.

此处的示例.这是代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://www.json.org/json_parse.js"></script>
<script type="text/javascript">
      $(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 +=  ']';

            var result = json_parse($strJson);

            $.each(result, function(key, item) {
             alert("Code: " + item.Code + " ,Name: " + item.Name + " ,Country: " + item.Country);
             //Taken the following out as it prevents objects after the first from being "alerted"
             //if ( key == 0) return false; //Prevent infinity loop
             });
      });
</script>
<title>Sandbox</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
    <p>Example Page</p>
</body>
</html>

这篇关于ASP.NET + jQuery,如何反序列化JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆