从MVC行动C#类反序列化JSON字符串 [英] Deserialize Json string from MVC action to C# Class

查看:201
本文介绍了从MVC行动C#类反序列化JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ajax调用(对于HighChartsDB图)在Web窗体应用程序调用Web服务,它使用的WebRequest 来调用它返回另一个应用程序的MVC行动一个JsonResult。

我设法将数据传回给ajax调用,但我取回的数据不会被解析为JSON对象,但只是一个字符串。

我的类:

 公共类CategoryDataViewModel
{
    公共字符串名称{;组; }
    公开名单< INT>数据{获得;组; }
    公共双pointStart {获得;组; }
    公众诠释pointInterval {{返回86400000; }}
}
 

我所谓的highcharts AJAX功能:

 函数getBugs(mileId){
    VAR的结果;

    $阿贾克斯({
        键入:GET,
        网址:服务/ WebService.svc /的GetData',
        的contentType:应用/ JSON的;字符集= UTF-8,
        异步:假的,
        数据:{mileId形式:parseInt(mileId)},
        数据类型:JSON,
        成功:功能(数据){
            的console.log(数据);
            结果=数据;
        }
    });

    返回结果;
}
 

最后我的WebService

 公共类的WebService:IWebService
{
    公共字符串的GetData(INT mileId)
    {
        字符串的URL =HTTP://本地主机:63418 /首页/ GetWoWData mileId =+ mileId;
        WebRequest的WR = WebRequest.Create(URL);

        使用(VAR响应=(HttpWebResponse)wr.GetResponse())
        {
            使用(VAR读卡器=新的StreamReader(response.GetResponseStream()))
            {
                变种objText = reader.ReadToEnd();
                返回objText;
            }
        }

    }
}
 

通过这个当我CONSOLE.LOG在Ajax调用我得到的(数据):

<$p$p><$c$c>[{\"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对象。我试图将其转换回我CategoryDataViewModel这个在我的web服务:

  VAR myojb =新CategoryDataViewModel();
    使用(VAR响应=(HttpWebResponse)wr.GetResponse())
    {
        使用(VAR读卡器=新的StreamReader(响应.GetResponseStream()))
        {
            JavaScriptSerializer JS =新JavaScriptSerializer();
            变种objText = reader.ReadToEnd();
            myojb =(CategoryDataViewModel)js.Deserialize(objText的typeof(CategoryDataViewModel));
        }
    }

    返回myojb;
 

但后来我得到键入Test.CategoryDataViewModel不支持数组的反序列化。

解决方案

修改

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

  myojb =(名单&LT; CategoryDataViewModel&GT;)js.Deserialize(objText的typeof(名单&LT; CategoryDataViewModel&GT;));
 

和你应该罚款。该阵列将反序列化到一个列表中没有任何问题。

I have an Ajax call (for a HighChartsDB chart) in a WebForm application that calls a webservice, which uses a WebRequest to call an MVC action in another application which returns a JsonResult.

I manage to pass the data back to the ajax call but the data I get back is not parsed as a json object but just a string.

My class:

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

My ajax function called by the 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;
}

And finally my 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;
            }
        }

    }
}

With this when I console.log(data) on the ajax call I get:

[{\"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}]

I can't seem to manage to pass back into a proper Json object. I tried converting it back to my CategoryDataViewModel with this in my webservice:

    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;

But then I get Type 'Test.CategoryDataViewModel' is not supported for deserialization of an array.

解决方案

Change:

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

to:

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

and you should be fine. The array will de-serialize to a list no problem.

这篇关于从MVC行动C#类反序列化JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆