JSON--WEB SERVICE

Query ajax webservice: get and post 
a, GET way 
the client 

Copy the code code is as follows:

var data = {classCode: "0001 "}; // here to directly JOSN objects 
$ .ajax ({ 
type: "the GET", 
contentType: "file application / JSON; charset = UTF-. 8", 
URL: "/ the WebServices / ProductPropertyWebService.asmx / GetProductPropertyList ", 
dataType:" JSON ", 
anysc: to false, 
Data: Data, 
Success: RenderProperties, 
error: function (the XMLHttpRequest, textStatus, errorThrown) { 
Alert (errorThrown + ':' + textStatus); // error processing 

}); 


Server-side 
Code 

Copy the code code is as follows:

[The ScriptMethod (ResponseFormat.Json the ResponseFormat =, = UseHttpGet to true)] // = UseHttpGet to true 
public List <Property> GetProductPropertyList () 

String classCode HttpContext.Current.Request = [ "classCode"]; // the Get embodiment, the query to string parameter values obtained in 
return PropertyManager.GetPropertySet (classCode, "the CN-ZH") the DataList;. 


Two, POST mode 
client 
codes 

Copy the code code is as follows:

var data = '{classCode: " ' + classCode + '", city: "GuangDong"}'; // to be used here spliced JOSN string 
$ .ajax ({ 
type: "the POST", 
contentType: "file application / JSON; charset = UTF-. 8 ", 
URL:" /WebServices/ProductPropertyWebService.asmx/GetProductPropertyList " 
dataType:" JSON " 
anysc: to false, 
Data: Data, Post // embodiment, data parameter can not be blank" ", if parameter does not pass, also written as "{}", otherwise it will not be attached contentType the Request Headers. 
Success: RenderProperties, 
error: function (the XMLHttpRequest, textStatus, errorThrown) { 
Alert (errorThrown + ':' + textStatus); // error handling 

}); 


Server-side 
Code 

Copy the code code is as follows:

[The ScriptMethod (ResponseFormat.Json the ResponseFormat =, = UseHttpGet to false)] // = UseHttpGet to false 
public List <Property> GetProductPropertyList (classCode String, String City) // Post embodiment, the parameter field properties corresponding to JSON, directly and automatically assign 

return PropertyManager.GetPropertySet (classCode, "the CN-ZH") the DataList;. 


Note: GET method and POST methods, there are parameters of time, if the parameter value is not ASCII characters (such as Chinese), GET parameters to encodeURI coding, or receives service data is garbled. 
Complex data submitted Json 
simple Json data format as {name: Yangjun, age: 27  }
data format Json complex, is only Json nested, such as: {name: yangjun, age: 27, child: [{name : yangke, age: 1},  {name: yangbin, age: 2}]}
If the data format is such complex Json to submit and obtain in Webservices then Json string according to the format, into a sequence. net objects, how to do it? 
For example, I want to submit the following data: 
Client: 
Code 

Copy the code code is as follows:
















Server-side: 
1 to deserialize .net objects as Json character, there are more open-source library, I use the .net version 3.5 comes with more than DataContractJsonSerializer, write a helper class: 
Code 

Copy the code code is as follows:

/// <Summary> 
/// Json serialization and deserialization helper methods 
/// </ Summary> 
public class JsonHelper 

/// <Summary> 
/// the JSON serialized: the sequence into an object format Json string 
/// </ Summary> 
public static string JsonSerializer <T> (T T) 

var Ser new new the DataContractJsonSerializer = (typeof (T)); 
var = new new MS the MemoryStream (); 
ser.WriteObject (MS, T); 
Encoding.UTF8.GetString JSONString = string (ms.ToArray ()); 
ms.Close (); 
return JSONString; 

/// <Summary> 
/// deserialize the JSON: Json character string format, into deserialized Object 
/// </ Summary> 
public static JsonDeserialize T <T> (String JSONString) 

var ser = new DataContractJsonSerializer(typeof(T)); 
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 
var obj = (T)ser.ReadObject(ms); 
return obj; 


2, because the respective deserialized into objects, so to construct two object classes, each note foregoing classes and properties modifier fields: 
Code 

Copy the code code is as follows:

[DataContract] 
public class MProductProperty 

[DataMember(Order = 0, IsRequired = true)] 
public int ProductId { set; get; } 
[DataMember(Order = 1, IsRequired = true)] 
public List<MProperty> PropertyList { set; get; } 

public class MProperty 

[DataMember(Order = 0, IsRequired = true)] 
public int PropertyId { set; get; } 
[DataMember(Order = 1, IsRequired = true)] 
public string PropertyType { set; get; } 
[DataMember(Order = 2, IsRequired = true)] 
public string PropertyValue { set; get; } 


3, Web Json method of receiving and processing data: 
Code 

Copy the code code is as follows:

[The WebMethod] 
[the ScriptMethod (= UseHttpGet to true)] 
public String PostProductPropertyList () 

String JSONString HttpContext.Current.Request = [ "propertyList"]; 
var = JsonHelper.JsonDeserialize productProperty <MProductProperty> (JSONString); // productProperty successfully deserialize MProductProperty object into 
// return identifying successful reception 
return "postsuccess"; 

You may also be interested in the article:

Guess you like

Origin www.cnblogs.com/wfy680/p/12174410.html