Serialization and deserialization of JSON in ASP.-----JavaScriptSerializer, plus your work experience

Source: Internet
Author: User

In the work and mobile communication with the Web services and JavaScriptSerializer, return JSON data for the phone to call, the beginning of the data returned is a lot, such as

[{"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 "} ]

The method used is

This method is to get word related information

[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 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;
}

The data obtained by the author below should be [{"Name": "Wanwan", "age:65"},{"name": "Xiaohua", "age:25"}], in this form, instead of a bracket, a curly brace, After observing the above observations combined with the following article, it is found that an object represents a piece of data, so the above should be changed to

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);
}
}

In this way, each time the loop, new an object, and then add the object to the list collection, loop after the list collection of a lot of Hashtable objects, each object in the future is a curly brace of data, after the serialization is finished is the following style

[{"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 "}]

The following is the original author's

Namespaces:

Asp. NET JSON serialization and deserialization can also use JavaScriptSerializer, under the System.Web.Script.Serializatioin namespace, you need to reference System.Web.Extensions.dll .

Instance:

class people
{
public int Id {get; set;}
public string Name {get; set;}
Public DateTime age {get; set;}
}

Serialization:

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);

Show Results:

[

{"Id": 1, "Name": "Zhangsan", "Age": "\/date (1363336335448) \"},

{"Id": 2, "Name": "Lisi", "Age": "\/date (1363336335448) \"}

]

Deserialization:

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 ("}

Foreground script parsing:

$ (": 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+ "});

});
});
return false;
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.