標籤:jquery javascript 面試 web json
JSON是什嗎?
JSON(JavaScript對象標記法), 是在網路通訊下,常用的一種資料表達格式,它有助於我們於一個自描述的,獨立的和輕的方式呈現並交換資料。這些資料可以易於和轉換為JavaScript對象。
650) this.width=650;" height="176" src="http://images.cnitblog.com/i/139239/201406/191028153019649.jpg" />
JSON格式的最大優點: 它可以被很容易得被轉換為一個javascript對象。例如,下面的程式碼片段中看到的,我們有一個JSON格式的資料裡面有“姓名”,“街道”,“年齡”和“手機”。
<script type="text/javascript"> var JSONObject= {"name":"John Johnson","street":"Oslo West 555", "age":33,"phone":"555 1234567"};alert(JSONObject.name); </script>
現在,這個資料可以直接使用,如我們可獲得JSONObject的對象,並很方便的調用其“name”屬性。
是否用SOAP可以做JSON同樣的事情呢?
有2點不同:
首先,SOAP是用XML標籤表達,比較重。JSON比較輕,純資料。
其次,最重要的是,JSON可直接轉換為javascript對象。同樣的要轉換SOAP XML為javascript對象是較繁瑣的任務。
650) this.width=650;" width="359" height="348" title="untitled" style="padding-top:0px;padding-right:0px;padding-left:0px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;background-image:none;" alt="untitled" src="http://img1.51cto.com/attachment/201406/26/2369428_1403764390X3Lq.png" border="0" />
是否所有的技術都支援JSON?
是的,幾乎所有的資料交換都支援JSON。
如下所示:
WCF服務:如果你想你的WCF服務應該發送JSON訊息,而不是SOAP,可以設定“ResponseFormat”作為你的作業合約“WebMessageFormat.Json”。
[OperationContract][WebInvoke(Method="GET", UriTemplate="/GetData", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]string GetData();
MVC:如果你希望MVC返回JSON資料;通過調用Json,可返回JSON格式的Customer對象。
public JsonResult CustomerJson(){ List<Customer> obj1 = new List<Customer>(); Thread.Sleep(5000); Customer obj = new Customer(); obj.CustomerCode = "1001"; obj1.Add(obj);return Json(obj1,JsonRequestBehavior.AllowGet);}
ASP.NET: 如果你想使用ASP.NET返回JSON,我們需要使用“DataContractJsonSerializer”類。
DataContractJsonSerializer serializer = new DataContractJsonSerializer(myPerson.GetType());MemoryStream ms = new MemoryStream();serializer.WriteObject(ms, myPerson);string json = System.Text.Encoding.UTF8.GetString(ms.ToArray());Response.Clear();Response.ContentType = "application/json;charset=utf-8";Response.Write(json);Response.End();
使用jQuery如何進行一個JSON調用?
如在一個MVC中,可通過“getEmployee”函數返回employ的JSON對象。
public JsonResult getEmployee(){Emp obj = new Emp();obj.empcode = "1001";return Json(obj,JsonRequestBehavior.AllowGet);}
我們用jQuery的getJson函數,來調用上述MVC函數返回的JSON對象即可。
$.getJSON("/Employee/getEmployee", null, getData);function getData(data){alert(data.empcode);}
jQuery的getJson函數有三個參數:
因為輸出是JSON,它會自動將JSON資料轉換為JavaScript對象。你可直接使用“getData”函數顯示了“data.empcode”屬性,非常便捷。
是否可以將JSON對象Post到伺服器?
可以。
可以使用jQuery的“post”方法將資料發送到伺服器。
下面是如何post方法調用的例子,同樣的,post有3個參數:
var mydata ={name:"Shiv",city:"Mumbai"};$.post("/Send/Request", // URLmydata , // Data to be sentfunction(data,status){alert(data + “ “ + status);}); // Call back function如何把一個完整的HTML表單以JSON格式Post到伺服器上?
首先,我們需要調用jQuery的”serialize“,把需要post的表單序列化為JSON對象。
然後,採用上面類似的辦法進行post即可。
完整代碼如下:
var Mydata = $("#form1").serialize();$.post("/Customer/getCustomer",JSON. stringify (MyData), DisplayData);
在後台(伺服器),可通過inputStream進行捕獲JSON資料流。
System.IO.Stream body = Request.InputStream;System.IO.StreamReader reader = new System.IO.StreamReader(body);string s = reader.ReadToEnd() ;
我們怎樣才能把JSON字串轉換為C#對象?
要轉換一個JSON字串到C#對象,我們需要使用“JavaScriptSerializer”類,並使用“還原序列化”,我們將字串轉換為C#對象。
var jsonser = new JavaScriptSerializer()var obj = jsonser.Deserialize<dynamic>(JsonString);foreach (var x in obj){ String strvalue = x[“value”];}
本文出自 “葡萄城控制項部落格” 部落格,請務必保留此出處http://powertoolsteam.blog.51cto.com/2369428/1431175