Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将MVC操作中的Json字符串反序列化为C#类_C#_Ajax_Web Services_Json - Fatal编程技术网

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

将MVC操作中的Json字符串反序列化为C#类,c#,ajax,web-services,json,C#,Ajax,Web Services,Json,我在调用webservice的WebForm应用程序中有一个Ajax调用(针对HighChartsDB图表),该应用程序使用WebRequest在另一个返回JsonResult的应用程序中调用MVC操作 我设法将数据传回ajax调用,但我得到的数据不是作为json对象解析的,而是一个字符串 我的班级: public class CategoryDataViewModel { public string name { get; set; } public List<int&g

我在调用webservice的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; } }
}
最后是我的网络服务

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(数据)时,我得到:

[{\"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对象。我尝试在我的Web服务中将其转换回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;
但随后我发现反序列化数组不支持类型“Test.CategoryDataViewModel”。更改:

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

myojb=(List)js.反序列化(objText,typeof(List));

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

我以前见过类似的情况,我认为您可能需要将myObj更改为列表

List<CategoryDataViewModel> myObjs = new List<CategoryDataViewModel>();
...
myObjs = js.Deserialize<List<CategoryDataViewModel>>(objText);
List myObjs=new List();
...
myObjs=js.反序列化(objText);

myojb也需要被重新定义为一个列表。该死的,我确实尝试过了,但是我把ajax部分的a
result=JSON.stringyfy(数据)
搞错了。。。谢谢!:)向乔伊道歉,他先到了,但出于某种原因,没有提醒我任何答案。
myojb = (List<CategoryDataViewModel> )js.Deserialize(objText, typeof(List<CategoryDataViewModel>));
List<CategoryDataViewModel> myObjs = new List<CategoryDataViewModel>();
...
myObjs = js.Deserialize<List<CategoryDataViewModel>>(objText);