How to pass JSON data and convert to Object in WebSerivce

左心房为你撑大大i 提交于 2019-12-25 02:13:47

问题


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";
}

回答1:


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";
}



回答2:


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.



来源:https://stackoverflow.com/questions/7925264/how-to-pass-json-data-and-convert-to-object-in-webserivce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!