JSON format for simple and complex parameter types and results for C # WebService

Source: Internet
Author: User

jquery as a good JS framework, easy-to-use features do not have to say. During the actual development process, an AJAX function called WebService with JQ

interface to realize the function of Ajax has become a more common technical means. The implementation of the WebService interface is usually implemented by the OOP language. So

In WebService interface functions, it is inevitable that complex data types, except for simple data types, may be encountered. The data type machine for complex data can be

The parameters in the WebService interface, and possibly the return value of the WebService. The main points described in this paper are:

1, for the WebService interface complex type parameters, JQ call when the incoming JSON data should be represented.

2. JQ gets the JSON data type for the WebService call.

3. What are the requirements for the complex data types returned by WebService when the JQ is called?

Environment: JQ version: 1.4.2, VS2008 SP1.

Test one: For WebService simple parameter types:

Copy CodeThe code is as follows: The WebService interface function code is as follows:
[WebMethod (Description = "test method")] public string processpersonalinfo (person person) {return person. Name + person.     Tel; The JQ invocation code is as follows:
$.ajax ({
Type: "POST",
URL: "Webservice1.asmx/getstring",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Data: "{' name ': ' Zhangsan '}",
Success:function (JSON) {alert (JSON.D)},
Error:function (Error) {
Alert ("Call error" + Error.responsetext);
}     });

Tip: In the $.ajax function, data must be represented as a string of JSON, not directly in JSON data. Some friends may have a string of JSON objects and JSON objects

It is not very good to differentiate, in fact, that strings are similar to what is generated in C #, and JSON objects are written directly in {}. The simple test method is popped directly through the alert function, if [Object:object] is displayed

is a JSON object, otherwise it is a string.

Results such as:

Test two: For WebService complex parameter types:

Copy CodeThe code is as follows: The WebService interface function code is as follows:
[WebMethod (Description = "test method")] public string processpersonalinfo (person person) {return PE Rson. Name + person.         Tel; }
Person Entity:
public class Person {public string Name {get; set;}
public int Age {get; set;}
public string Address {get; set;}
public string Tel {get; set;}
}
The JQ invocation code is as follows:
$.ajax ({
Type: "POST",
URL: "Webservice1.asmx/processpersonalinfo",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Data: ' {' person ': {' Name ': ' Zhangsan ', ' age ': ' Address ': ' Beijing ', Tel: ' 01082658866 '} ',
Success:function (JSON) {alert (JSON.D)},
Error:function (Error) {
Alert ("Call error" + Error.responsetext); }         });

  Results such as:

The calling procedure is similar to a simple parameter type, that is, when sent to the client by using a string in JS that represents the person object of person, WebService automatically converts the string of the person object

Converts to a person entity object.

Test three: For WebService complex return types

Copy CodeThe code is as follows: The WebService interface function code is as follows:
        [WebMethod (Description = "test Method")]          Public list<person> getpersonallist ()         {             list<person> persons = new List<person>                                       {                                          New Person {Address = ' Beijing ', age = +, Name = ' Zhangshan ', Tel = ' 01082678866 '} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N Bsp;                            };             return persons;        }<br> JQ Invocation code is as follows:
$.ajax ({
Type: "POST",
URL: "Webservice1.asmx/getpersonallist",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Success:function (JSON) {$ (JSON.D). each (function () {alert (this. Name + "-" + this. Age + "-" + this. Address + "-" + this. Tel)}),
Error:function (Error) {
Alert ("Call error" + Error.responsetext);
}
});

Such as:

That is, for complex return types, the processing is basically the same as the simple type.

I have heard the idea that the return data type must be serializable when the JQ invocation is Websevice and JSON is used as the data Interchange format. Is that really the case?

. NET's basic data types are indeed serializable, without doubt. So can the List<t> data type be serialized?? See list<t> Meta data (Metadata) information

I knew it.

[DebuggerTypeProxy (typeof (Mscorlib_collectiondebugview<t>))]

[DebuggerDisplay ("Count = {count}")]

[Serializable]

public class list<t>: Ilist<t>, Icollection<t>, Ienumerable<t>, IList, ICollection, IEnumerable

{

/**/

}

If the above statement is established, in this case, the invocation of success is understandable. But is that really the problem? Let's continue testing:

Test four: For WebService complex return types

Copy CodeThe code is as follows: [WebMethod (Description = "test method")] public person Getperson () {Person person = new person {<br> Address = "Beijing", age =, <BR> Name = "             Zhangshan ", Tel =" 01082678866 "<BR>};         return person; }
The JQ invocation code is as follows:
$.ajax ({
Type: "POST",
URL: "Webservice1.asmx/getperson",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Data: ' {' person ': {' Name ': ' Zhangsan ', ' age ': ' Address ': ' Beijing ', Tel: ' 01082658866 '} ',
Success:function (JSON) {$ (JSON.D). each (function () {alert (this. Name + "-" + this. Age + "-" + this. Address + "-" + this. Tel)}),
Error:function (Error) {
Alert ("Call error" + Error.responsetext);
}
});

Such as:

But in Test four, the Getperson () method returns the person data type. Then look at the definition of the person entity, there is no tag to ask Serializable.

As the result shows: JQ calls WebService, and data that does not necessarily need to return complex types must be serializable.

Here's an interesting test. We all know that the return type of WebService cannot be a hashtable type. Because it implements the IDictionary interface because it implements it.

Test five: For WebService complex return types

Copy CodeThe code is as follows: [WebMethod (Description = "test method")] public Hashtable getpersonalhashtable () {Hash Table Hashtable = new Hashtable ();
person person = new Person {Address = ' Beijing ', age = +, Name = ' Zhangshan ', Tel = ' 01082678866 '};
Hashtable. ADD (1, person);
return Hashtable; }
The JQ invocation code is as follows:
$.ajax ({
Type: "POST",
URL: "Webservice1.asmx/getpersonalhashtable",
DataType: "JSON",
ContentType: "Application/json; Charset=utf-8 ",
Data:data,
Success:function (JSON) {$ (JSON.D). each (function () {alert (this["one"]. Name})},
Error:function (Error) {
Alert ("Call error" + Error.responsetext);
}
});

 

In this way, JQ can actually invoke success. This is a bit of an unexpected one.

Summarize:

1, the WebService between JQ and the use of JSON as a form of data exchange, ContentType: "Application/json; Charset=utf-8 "is a must-specify.

Otherwise webservice does not know what data to use as the conversion.

2. JQ calls WebService returns a complex data type and does not necessarily require the type to be serializable.

3. The JSON data returned by WebService is obtained by ". D" as alert (JSON.D) in the above test.

JSON format for simple and complex parameter types and results for C # WebService

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.