将Json字符串从MVC操作反序列化为C#类

时间:2011-08-20 00:44:48

标签: c# ajax web-services json

我在调用web服务的WebForm应用程序中有一个Ajax调用(对于HighChartsDB图表),它使用WebRequest在另一个返回JsonResult的应用程序中调用MVC操作。

我设法将数据传递回ajax调用,但是我得到的数据不会被解析为json对象而只是一个字符串。

我的课程:

public class CategoryDataViewModel
{
    public string name { get; set; }
    public List<int> data { get; set; }
    public double pointStart { get; set; }
    public int pointInterval { get { return 86400000; } }
}

我的ajax函数由highcharts调用:

function getBugs(mileId) {
    var results;

    $.ajax({
        type: 'GET',
        url: 'Services/WebService.svc/GetData',
        contentType: "application/json; charset=utf-8",
        async: false,
        data: { "mileId": parseInt(mileId) },
        dataType: "json",
        success: function (data) {
            console.log(data);
            results = data;
        }
    });

    return results;
}

最后是我的WebService

public class WebService : IWebService
{
    public string GetData(int mileId)
    {
        string url = "http://localhost:63418/Home/GetWoWData?mileId=" + mileId;
        WebRequest wr = WebRequest.Create(url);

        using (var response= (HttpWebResponse)wr.GetResponse())
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var objText = reader.ReadToEnd();
                return objText;
            }
        }

    }
}

当我在ajax调用上调试console.log(data)时,我得到:

[{\"name\":\"Sedan\",\"data\":[30,30,30,30,35],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Low\",\"data\":[800,800,800,826,1694],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Medium\",\"data\":[180,180,180,186,317],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"High\",\"data\":[29,29,29,34,73],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Truck\",\"data\":[6,6,6,6,13],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"SUV\",\"data\":[-172,-172,-172,-179,-239],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Convertible\",\"data\":[0,0,0,0,-404],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Limo\",\"data\":[-7,-7,-7,-8,-214],\"pointStart\":1307836800000,\"pointInterval\":86400000}]

我似乎无法设法传回适当的Json对象。我尝试在我的webservice中将它转换回我的CategoryDataViewModel:

    var myojb = new CategoryDataViewModel ();
    using (var response = (HttpWebResponse)wr.GetResponse())
    {
        using (var reader = new StreamReader(response .GetResponseStream()))
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var objText = reader.ReadToEnd();
            myojb = (CategoryDataViewModel )js.Deserialize(objText, typeof(CategoryDataViewModel ));
        }
    }

    return myojb;

然后我得到Type 'Test.CategoryDataViewModel' is not supported for deserialization of an array.

2 个答案:

答案 0 :(得分:1)

变化:

myojb = (CategoryDataViewModel )js.Deserialize(objText, typeof(CategoryDataViewModel ));

为:

myojb = (List<CategoryDataViewModel> )js.Deserialize(objText, typeof(List<CategoryDataViewModel>));

你应该没事。该数组将反序列化为列表没有问题。

答案 1 :(得分:1)

之前我见过类似的事情,我想你可能需要把myObj改成一个列表。

List<CategoryDataViewModel> myObjs = new List<CategoryDataViewModel>();
...
myObjs = js.Deserialize<List<CategoryDataViewModel>>(objText);