ONYX
ONYX

Reputation: 5859

How to pass JSON data and convert to Object in WebSerivce

I want to be able to convert a JSON object that is passed through ajax(jquery) in my webservice. At the moment I can get it to return the message "Hello World" but I don't know how to access the passed JSON data and then convert to a collection of type IList so I can iterate over the collection. I've had a look around on stackoverflow but I'm getting confused what to do can some one help me.

Here is my code:

jQuery:

var data = { dvals: [{'Name' : 'Acer', 'Count' : '2'}, {'Name' : 'HP', 'Count' : '4'} ] };

function getProducts(json, pageIndex, pageSize) {
    json["PageIndex"] = pageIndex;
    json["PageSize"] = pageSize;

    $.ajax({
        type: 'POST',
        url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
        data: JSON.stringify(json),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (responseText) {
            //alert(responseText.d);
            $('.console').html(responseText.d);
        },
        error: function (xhr, status, error) {
            //var msg = JSON.parse(xhr.responseText);
            //alert(msg.Message);
            $('.console').html(xhr.responseText)
        }
    });
}
getProducts(data, '0', '2');

My asp.net C#:

public class Filter
{
    public string Name;
    public int Count;
}
public class Product
{
    public int Id;
    public string Title;
    public string ShortDescription;
    public string Brand;
    public string Model;
    public double SellPrice;
    public string DescountPercentage;
    public int Votes;
    public int TotalRating;
    public double Rating
    {
        get
        {
            return Votes / TotalRating;
        }
    }
}

public class FiltersAndProducts
{
    List<Filter> Filters;
    List<Product> Products;
    int PageIndex;
    int PageSize;
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsAndFilters()
{        
    return "Hello World";
}

Upvotes: 0

Views: 2088

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Note the various ways to call it here: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

Its all about how you define what is going to GetProductsAndFilters. You can pass as a list or pass as a DTO.

Upvotes: 3

Rafay
Rafay

Reputation: 31033

if you make a class like

public class dvals{

public string Name{get;set;}
public string Count{get;set;}

}

prepare json

var dvals =[{Name:'Acer',Count:'2'},{Name:'HP',Count:'4'}];
dval=JSON.stringify(dvals);

send via ajax

$.ajax({
        type: 'POST',
        url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
        data: dval,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (responseText) {
            //alert(responseText.d);
              console.log(responseText);
            $('.console').html(responseText.d);
        },
        error: function (xhr, status, error) {
            //var msg = JSON.parse(xhr.responseText);
            //alert(msg.Message);
            $('.console').html(xhr.responseText)
        }
    });

webservice side

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsAndFilters(IList<dvals> dvalsList)
{        
    return "Hello World";
}

Upvotes: 2

Related Questions