jquery Ajax Call WebService returns a JSON array

Source: Internet
Author: User
Tags foreach json

This article is using jquery Ajax call WebService to return the JSON array Oh, JSON data is a small lightweight data for web effects, and real-time interactivity is stronger than XML.

JSON data

{' Employee ': [{' Name ': ' John ', ' Sex ': ' Man ', ' age ': '},{' ' name ': ' Tom ', ' Sex ': ' Mans ', ' age ': '},{' name ': ' Mary ', ' Sex ': ' Woman ', ' Age ': '}
'}

jquery calls WebService import data
function LoadData () {
var studentdata = Collectiondata ();
$.ajax ({
URL: "Importdataservice.asmx/importstu",
Type: "Post",
ContentType: "Application/json;charset=utf-8",
DataType: "JSON",
Data: "{' Students ': [{' name ': ' Kobe ', ' sex ': ' Boy ', ' age ': '},{' name ': ' Mary ', ' Sex ': ' Girl ', ' age ': ' 19 '}]} ',
Success:function (Result) {
alert (RESULT.D);
},
Error:function (e) {
alert (E.responsetext);
}
});
}

<summary>
///
</summary>
<param name= "Students" ></param>
<returns></returns>
[WebMethod]
[Scriptmethod (Responseformat=responseformat.json)]
public string Importstu (dictionary<string,string> []students)
{
if (students.length = 0)
{
Return "No data!"
}
Else
{
Try
{
foreach (dictionary<string, string> stu in students)
{
Constructs a new student object.
Student student = new Student ();

Assigns a value to the newly constructed student object property.
foreach (string key in Stu.keys)
{
Switch (key)
{
Case "Name":
Student.name = Stu[key];
Break
Case "Sex":
Student.sex = Stu[key];
Break
Case "Age":
int age;
if (Int32.TryParse (Stu[key], out of age)
{
Student.age = age;
}
Else
{
student.age = 0;
}
Break
Default
Break
}
}
}
Return "Import Student success!"
}
Catch
{
Throw new Exception ("Failed to import student!");
}
}
}

Related Article

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.