为什么JavaScriptSerializer.Deserealize会产生空结果?

时间:2019-08-15 23:04:23

标签: c# asp.net json json-deserialization webmethod

我试图获取API的结果,以仅将选定的属性作为JSON字符串或对象返回。完整的返回JSON字符串如下所示:

{"ListEvents":[{"EventID":1,"EventName":"Debby 
2000","State":"PR","EventType":"Tropical or 
Extratropical","Days":5,"LSTStart":"\/Date(966643200000- 
0000)\/","LSTEnd":"\/Date(967075200000-0000)\/"},{"EventID":2,...]}

要做到这一点,似乎我需要使用Serializer并创建一些公共类来充当我的WebMethod函数外部的模板。

public class RII_Service : System.Web.Services.WebService
{
   HttpClient client = new HttpClient();
    string _appRootDir = HttpContext.Current.Server.MapPath("~/");
    public class Flood_Dataset
    {
       public int EventID { get; set; }
        public string EventName { get; set; }
        public string State { get; set; }
        public string EventType { get; set; }
    }
     public class ListEvent
    {
        public List<Flood_Dataset> ListEvents { get; set; }
    }

    public class RootObject
    { 
        public ListEvent Data_Response { get; set; }
    }
   ...

然后我在制作WebRequest并获得HttpWebResponse后,尝试通过序列化应用这些公共类:

public RootObject GetFloodData()
{
  WebRequest requestObj = WebRequest.Create(url);
  requestObj.Method = "GET";
  requestObj.ContentType = "application/json";
  requestObj.Timeout = 10 * 1000;
  HttpWebResponse responseObj = null;
  RootObject jsonResponse = null;
  JavaScriptSerializer returnJson = new JavaScriptSerializer();
  try
  {
     string strresult = null;
      responseObj = (HttpWebResponse)requestObj.GetResponse();
      using (Stream stream = responseObj.GetResponseStream())
      {
           StreamReader sr = new StreamReader(stream);
           strresult = sr.ReadToEnd();
           sr.Close();
      }

      jsonResponse = returnJson.Deserialize<RootObject>(strresult);
      return jsonResponse;
        ....
   }
}

当我在调试器中检查变量strresult时,看到"{\"EventID\":71,\"EventName\":\"Maria 2017\",...}"。但是当它到达 第jsonResponse = returnJson.Deserialize<RootObject>(strresult);行,我只看到Data_Response旁边的{recurrence_interval_app.RII_Service.RootObject}为空。我在这里做错了什么?我正在根据类似的post来建模我的解决方案。

0 个答案:

没有答案