在工作中和手机通信用到web服务和javascriptSerializer,返回json数据,供手机端调用,一开始返回的数据是一大堆,比如

[{"word_picture9":"http://boss.xbxw.net/Upload/word/guojia/p_yinggelan.jpg","word_picture8":"http://boss.xbxw.net/Upload/word/guojia/p_zhongguo.jpg","word_voice2":"http://boss.xbxw.net/Upload/voice/approve.mp3","word_picture13":"http://boss.xbxw.net/Upload/word/gaokao/p_qingxu.jpg","word_voice14":"http://boss.xbxw.net/Upload/voice/Laos.mp3","word_picture10":"http://boss.xbxw.net/Upload/word/jijie/p_chuntian.jpg","word_voice11":"http://boss.xbxw.net/Upload/voice/Tidy.mp3","word_voice1":"http://boss.xbxw.net/Upload/voice/Mexico.mp3","word_picture15":"http://boss.xbxw.net/Upload/word/gaokao/p_dizhixue.jpg","word_picture16":"http://boss.xbxw.net/Upload/word/gaokao/p_youhuo.jpg","word_voice17":"http://boss.xbxw.net/Upload/voice/Austria.mp3","word_voice10":"http://boss.xbxw.net/Upload/voice/Spring.mp3","word_voice3":"http://boss.xbxw.net/Upload/voice/Giant.mp3","word_voice0":"http://boss.xbxw.net/Upload/voice/Fertilizer.mp3","word_picture12":"http://boss.xbxw.net/Upload/word/jiaotong/p_qianshuiting.jpg","word_picture17":"http://boss.xbxw.net/Upload/word/guojia/p_aodili.jpg","word_voice16":"http://boss.xbxw.net/Upload/voice/Temptation.mp3","word_voice6":"http://boss.xbxw.net/Upload/voice/Oxygen.mp3","word_voice12":"http://boss.xbxw.net/Upload/voice/submarine.mp3","word_voice13":"http://boss.xbxw.net/Upload/voice/mood.mp3","word_voice4":"http://boss.xbxw.net/Upload/voice/Volunteer.mp3","word_picture14":"http://boss.xbxw.net/Upload/word/guojia/p_laowo.jpg","word_voice5":"http://boss.xbxw.net/Upload/voice/label.mp3","word_voice8":"http://boss.xbxw.net/Upload/voice/china.mp3","word_voice9":"http://boss.xbxw.net/Upload/voice/England.mp3","word_picture11":"http://boss.xbxw.net/Upload/word/gaokao/p_zhegnjiede.jpg","word_voice7":"http://boss.xbxw.net/Upload/voice/Dusk.mp3","word_picture5":"http://boss.xbxw.net/Upload/word/gaokao/p_biaoqian.jpg","word_picture4":"http://boss.xbxw.net/Upload/word/gaokao/p_zhiyuanzhe.jpg","word_picture7":"http://boss.xbxw.net/Upload/word/gaokao/p_huanghun.jpg","word_picture6":"http://boss.xbxw.net/Upload/word/gaokao/p_yangqi.jpg","word_picture1":"http://boss.xbxw.net/Upload/word/guojia/p_moxige.jpg","word_picture0":"http://boss.xbxw.net/Upload/word/gaokao/p_feiliao.jpg","word_picture3":"http://boss.xbxw.net/Upload/word/gaokao/p_juren.jpg","word_picture2":"http://boss.xbxw.net/Upload/word/gaokao/p_pizhun.jpg","word_voice15":"http://boss.xbxw.net/Upload/voice/Geology.mp3"}]

用的方法是

///这个方法是得到单词相关信息

[WebMethod]
public string getWord(int ban)
{
CoDBHelper helper = new CoDBHelper("Server=120.192.19.41;database=KSXT;uid=sa;pwd=#rl42s3cx");
DataSet ds = helper.ExecuteQueryDS("select top 18 word_picture,word_voice from word where ban=" + ban + " order by newid()");
DataTable dt = new DataTable();
dt = ds.Tables[0];
Hashtable ht = new Hashtable();
ArrayList eventList = new ArrayList();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{

string word_picture = dt.Rows[i]["word_picture"].ToString();
string word_voice = dt.Rows[i]["word_voice"].ToString();
ht.Add("word_picture" + i, word_picture);
ht.Add("word_voice" + i, word_voice);

}
}
eventList.Add(ht);

JavaScriptSerializer ser = new JavaScriptSerializer();
String jsonStr = ser.Serialize(eventList);
return jsonStr;
}

通过下面原作者得到的结果看得到的数据应该是[{"name":"wanwan","age:65"},{"name":"xiaohua","age:25"}],这样的形式,而不是一个中括号,一个大括号了事,经过对上面的观察结合下面的文章,发现一个对象代表一条数据,所以,上面的应该改成

Hashtable ht = null;

ArrayList eventList = new ArrayList();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
ht=new HashTable()
string word_picture = dt.Rows[i]["word_picture"].ToString();
string word_voice = dt.Rows[i]["word_voice"].ToString();
ht.Add("word_picture" + i, word_picture);
ht.Add("word_voice" + i, word_voice);
eventList.Add(ht);
}
}

这样,每次循环一次,new一个对象,然后对象添加到list集合里,循环玩后list集合里就集中了很多hashtable对象,每一个对象将来就是一个大括号里的数据,序列化完事后就是下面的样式

[{"word_picture0":"http://boss.xbxw.net/Upload/word/yueqi/p_xiaotiqin.jpg","word_voice0":"http://boss.xbxw.net/Upload/voice/violin.mp3"},{"word_voice1":"http://boss.xbxw.net/Upload/voice/drum.mp3","word_picture1":"http://boss.xbxw.net/Upload/word/yueqi/p_gu.jpg"},{"word_voice2":"http://boss.xbxw.net/Upload/voice/shovel.mp3","word_picture2":"http://boss.xbxw.net/Upload/word/shenghuo/p_chanzi.jpg"},{"word_picture3":"http://boss.xbxw.net/Upload/word/gaokao/p_fenmo.jpg","word_voice3":"http://boss.xbxw.net/Upload/voice/Powder.mp3"},{"word_voice4":"http://boss.xbxw.net/Upload/voice/Media.mp3","word_picture4":"http://boss.xbxw.net/Upload/word/gaokao/p_duomeiti.jpg"},{"word_picture5":"http://boss.xbxw.net/Upload/word/gaokao/p_luxianhangxian.jpg","word_voice5":"http://boss.xbxw.net/Upload/voice/Route.mp3"},{"word_picture6":"http://boss.xbxw.net/Upload/word/gaokao/p_wangluo.jpg","word_voice6":"http://boss.xbxw.net/Upload/voice/network.mp3"},{"word_picture7":"http://boss.xbxw.net/Upload/word/gaokao/p_guzhang.jpg","word_voice7":"http://boss.xbxw.net/Upload/voice/Applause.mp3"},{"word_voice8":"http://boss.xbxw.net/Upload/voice/Liberia.mp3","word_picture8":"http://boss.xbxw.net/Upload/word/guojia/p_libiliya.jpg"},{"word_picture9":"http://boss.xbxw.net/Upload/word/gaokao/p_qiyou.jpg","word_voice9":"http://boss.xbxw.net/Upload/voice/petrol.mp3"},{"word_voice10":"http://boss.xbxw.net/Upload/voice/Nylon.mp3","word_picture10":"http://boss.xbxw.net/Upload/word/gaokao/p_nilong.jpg"},{"word_picture11":"http://boss.xbxw.net/Upload/word/jiaotong/p_kache.jpg","word_voice11":"http://boss.xbxw.net/Upload/voice/Truck.mp3"},{"word_picture12":"http://boss.xbxw.net/Upload/word/guojia/p_baxi.jpg","word_voice12":"http://boss.xbxw.net/Upload/voice/Brazil.mp3"},{"word_voice13":"http://boss.xbxw.net/Upload/voice/sketch.mp3","word_picture13":"http://boss.xbxw.net/Upload/word/gaokao/p_sumiao.jpg"},{"word_picture14":"http://boss.xbxw.net/Upload/word/dianzi/p_shubiao.jpg","word_voice14":"http://boss.xbxw.net/Upload/voice/mouse.mp3"},{"word_voice15":"http://boss.xbxw.net/Upload/voice/deposit.mp3","word_picture15":"http://boss.xbxw.net/Upload/word/gaokao/p_cunkuan.jpg"},{"word_picture16":"http://boss.xbxw.net/Upload/word/jiaotong/p_tuolaji.jpg","word_voice16":"http://boss.xbxw.net/Upload/voice/tractor.mp3"},{"word_picture17":"http://boss.xbxw.net/Upload/word/jiaotong/p_silunmache.jpg","word_voice17":"http://boss.xbxw.net/Upload/voice/wagon.mp3"}]

下面是原来作者的

命名空间:

        ASP.NET中的JSON序列化和反序列化还可以使用JavaScriptSerializer,在System.Web.Script.Serializatioin命名空间下,需引用System.Web.Extensions.dll.

实例:

 class People
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Age { get; set; }
    }

序列化:

List<People> list = new List<People>();
        People peo = new People();
        peo.Id = 1;
        peo.Name = "zhangsan";
        peo.Age = DateTime.Now;
         list.Add(peo);

People peo2 = new People();
         peo2.Id = 2;
         peo2.Name = "lisi";
         peo2.Age = DateTime.Now;
         list.Add(peo2);

JavaScriptSerializer ser = new JavaScriptSerializer();

TextBox1.Text = ser.Serialize(list);

显示结果:

[

{"Id":1,"Name":"zhangsan","Age":"\/Date(1363336335448)\/"},

{"Id":2,"Name":"lisi","Age":"\/Date(1363336335448)\/"}

]

反序列化:

string json = TextBox1.Text;

JavaScriptSerializer ser=new JavaScriptSerializer();

List<People> list = ser.Deserialize<List<People>>(json);

foreach (People peo in list)
        {
            Response.Write(peo.Name + "<br>" + peo.Id + "<br>" + peo.Age);
            Response.Write("<hr>");
        }

前台脚本解析:

$(":submit:eq(2)").click(function () {
                // return false;
                $.getJSON("../logic/October.ashx", { code: "one" }, function (data) {
                    $.each(data, function (key, val) {
                        $("p").append(key + "--->>" + val + "<br>");
                        $.each(val, function (k, v) {
                            $("h5").append(k+"---->>"+v+"<hr>");
                        });
                        
                    });
                });
                return false;
            });

Asp.Net中JSON的序列化和反序列化-----JavaScriptSerializer ,加上自己工作心得的更多相关文章

  1. Asp.net中Json的序列化和反序列化(一)

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  2. ASP.NET中JSON的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...

  3. ASP.NET 中JSON 的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  4. ASP.NET中JSON的序列化和反序列化(转)

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  5. [转]ASP.NET中JSON的序列化和反序列化

    本文转自:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html JSON是专门为浏览器中的网页上运行的JavaScript代码 ...

  6. Asp.net中Json的序列化和反序列化(二)

     三.JSON序列化和反序列化日期时间的处理 JSON格式不直接支持日期和时间.DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,其中第一个数字(在提供的示例中 ...

  7. ASP.NET MVC Json的序列化和反序列化

    1.利用js进行序列化成字符串和反序列化 var personObj = {name:"Tom",age:16}; // 利用JS序列化成字符串 var personStr = J ...

  8. .NET中JSON的序列化和反序列化的几种方式

    一.什么是JSON JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于ECMAScript(欧洲计算机协会制定的js规范)的一个子集 ...

  9. golang中json包序列化与反序列化

    package main import ( "encoding/json" "fmt" "reflect" ) type Info stru ...

随机推荐

  1. Ubuntu 添加sudo用户

    第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密 码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到 ...

  2. csv 导入到 access中去

    Csv中有500万数据,导入到Access中去,每6万条数据为1Table 先是参照着http://support.microsoft.com/kb/257819/zh-cn来写 1.找不到可安装的  ...

  3. nagios配置

    接近两个星期的奋战,nagios的安装搭建以及监控服务自动报警功能终于基本得以实现,现在自己整理一份安装技术手册,方便自己以后查阅和回顾. 接近两个星期的奋战,nagios的安装搭建以及监控服务自动报 ...

  4. JS将图片文件转为64位字符串再post到接口上传图片

    HTML: <div class="ai-item upload-id-img"> <p>上传身份证照片</p> <div class=& ...

  5. 线段树:Segment Tree(单点修改/区间修改模板) C++

    线段树是非常有效的数据结构,可以快速的维护单点修改,区域修改,查询最大值,最小值等功能. 同时,它也很重要.如果有一天比赛,你卡在了一道线段树模板题目上,这就真的尴尬了.不过,随着时代的进步,题目也越 ...

  6. The use of function Merge (update、insert、delete)

    1.The use of function merge(update.insert.delete) Example: #1.Initialize the data create table #test ...

  7. python 3.5 格式化字符串输出

    #!/usr/bin/env python #encoding: utf-8 #.strip('里面可以去掉字符串中两边的字符') name = input('name :').strip(' ') ...

  8. 用root帐号切换其他帐号提示 su: warning: cannot change directory to /home/oracle: Permission denied

    用root帐号切换其他帐号提示: 出错原因: 基本上是根目录或者是/home/oracle目录权限的问题 解决办法: 更改根目录权限为755,并保证对应用户主目录的所属用户和所属组一致和用户名一致. ...

  9. ServletContext对象(每个工程只有一个此对象)

    一]重点方法:        1>存取对象                        void setAttribute(String name, Object object);//将obj ...

  10. 【Xamarin 开发 IOS --IOS 页面导航概念Segue】

    Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom 一.视图切换类型介绍在storyboard中,segue有几种不同的类型, ...