如何在WebSerivce中传递JSON数据并将其转换为Obje

如何在WebSerivce中传递JSON数据并将其转换为Obje

本文介绍了如何在WebSerivce中传递JSON数据并将其转换为Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够转换通过我的Web服务中的ajax(jquery)传递的JSON对象.目前,我可以获取它返回消息"Hello World",但是我不知道如何访问传递的JSON数据,然后转换为IList类型的集合,因此可以遍历该集合.我已经了解了stackoverflow,但是我感到困惑,该怎么办才能帮助我.

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.

这是我的代码:

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');

我的asp.net C#:

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

推荐答案

如果您制作类似

public class dvals{

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

}

准备json

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

通过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)
        }
    });

网络服务端

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

这篇关于如何在WebSerivce中传递JSON数据并将其转换为Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:38