Asp.Net中JSON的序列化和反序列化-----JavaScriptSerializer ,加上自己工作心得

在工作中和手机通信用到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;
            });

上一篇:.NET中JSON的序列化和反序列化的几种方式


下一篇:iOS中的过期方法和新的替代方法