2

when we send data from server side to client side then data is automatically serialize & converted to json format and jquery can highly parse that json.

as a example

[WebMethod]
public static Person GetData()
{
   JavaScriptSerializer js = new JavaScriptSerializer();
   Person p1 = new Person();
   p1.firstName = "Rakki";
   p1.lastName = "Muthukumar";
   p1.technologies = new string[] { "IIS", "ASP.NET", "JavaScript", "AJAX" };
   return p1;
}

this above trick works and data converted to json format and get back to client then

but i saw people use JavaScriptSerializer class to convert the data in json format and then send the whole json to client side to parse by jquery.

a small sample for JavaScriptSerializer

[WebMethod]
public static string GetData()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Person p1 = new Person();
p1.firstName = "Rakki";
p1.lastName = "Muthukumar";
p1.technologies = new string[] { "IIS", "ASP.NET", "JavaScript", "AJAX" };
string str = js.Serialize(p1);
}

i just need to know why people use JavaScriptSerializer class to convert data in json format when data is automatically converted to json.

tell me the exact situation when one has to use JavaScriptSerializer to make data converted to json format.

please discuss this in detail....thanks

4

1 回答 1

0

我认为这是一种编码习惯或一种定向思维,甚至通过 .NET 框架帮助我们完成了这一步。

但另一方面,我们通常认为客户端只接受一些字符串值,如 json、xml 或一些普通文本。可能在我们看来,它们是一种声音,C# 类型与 javascript 类型不兼容,我们需要先转换它们才能使用它们。而且,我们认为这是获得我们想要的结果的安全方法。

于 2012-08-08T00:21:03.130 回答