Pass JSON Array to WebServices

You have to create a JSON string of Array Object and pass the Object to Web Service. Most Advantages of this type of calling functionality is not need to Type Conversation or Casting.

Step 1: Create Simple html page like.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Untitled Page</title>

    <script src="js/jquery-1.9.1.js" type="text/javascript"></script>

    <script type="text/javascript" src="js/json2.min.js"></script>

</head>

<body> 

    <script type="text/javascript">

         BindContactInfo();

        function BindContactInfo() {

            //Create an array of Contact Details class.

            var contact = new Array();

            for (var i = 0; i < 2; i++) {

                var ContactInfo = new Object();

                if (i == 0) {

                    ContactInfo["FirstName"] = "Jayendrasinh";

                    ContactInfo["LastName"] = "Gohil";

                    ContactInfo["Address"] = "A/74 Ankur";

                    ContactInfo["City"] = "Ahmedabad";

                    ContactInfo["State"] = "Guj";

                    ContactInfo["Zip"] = 382350;

                }

                else if (i == 1) {

                    ContactInfo["FirstName"] = "Pratipalsinh";

                    ContactInfo["LastName"] = "Gohil";

                    ContactInfo["Address"] = "A/74 Anour";

                    ContactInfo["City"] = "Vadodara";

                    ContactInfo["State"] = "Guj";

                    ContactInfo["Zip"] = 382350;

                }

                contact[i] = ContactInfo;

            }

            /* Create a data transfer object (DTO) with the  

            remember DTO  Parameter and Web Sevice Parameter name are same.

          */

            var DTO = { 'ContactInfo': contact };

            $.ajax({

    contentType: "application/json; charset=utf-8",

    data: JSON.stringify(DTO),

    dataType: "json",

    type: "POST",

    url: "ContactService.asmx/AddContactInfo" //Call the Web Service Method.

});

        }

    </script>

 

</body>

</html>

Step 2: Create class name ContactInfo

   

public class ContactInfo

{

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Zip { get; set; }

 

    public void Add()

    {

        // Save me here!

    }

}

Step 3: Create Web Service Name ContactService like

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

 

namespace OraclewithODPTest

{

    /// <summary>

    /// Summary description for ContactService

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

    [System.Web.Script.Services.ScriptService]

    public class ContactService : System.Web.Services.WebService

    {

        [WebMethod]

        public void AddContactInfo(List<ContactInfo> ContactInfo)

        {

            if (ContactInfo != null)

            {

                foreach (var item in ContactInfo)

                {

 

                }

            }

        }

    }

}

For more information you can download the source code.