为什么反序列化的 JSON 数组返回 null?

我有一个JSON从 API 返回数据的字符串流,如下所示:


"{\"Recs\": 

[

   {\"EID\":\"F67_24_6\",\"ReturnPeriod\":\"1\",\"GageStation\":\"NA\"}, 

   {\"EID\":\"T67_24_6\",\"ReturnPeriod\":\"2.37\",\"GageStation\":\"Magueyes Island\"}, 

   {\"EID\":\"R67_24_6\",\"ReturnPeriod\":\"1\",\"GageStation\":\"50147800\"}

]}"

我正在尝试反序列化它以返回:


{"Recs":[

          {"EID":"F67_24_6","ReturnPeriod":"1","GageStation":"NA"}, 

          {"EID":"T67_24_6","ReturnPeriod":"2.37","GageStation":"Magueyes Island"},     

          {"EID":"R67_24_6","ReturnPeriod":"1","GageStation":"50147800"}

]}

我使用这些公共类来构建返回:


public class New_Events_Dataset

{

    public string EID { get; set; }

    public string ReturnPeriod { get; set; }

    public string GageStation { get; set; }


}


public class NewRootObject

{

    public List<New_Events_Dataset> Reqs { get; set; }

}

当我稍后尝试应用此方法时,我基本上得到了{"Reqs":null}. 我在这里做错了什么?


var jsonResponse = JsonConvert.DeserializeObject<NewRootObject>(strresult);

string json = new JavaScriptSerializer().Serialize(jsonResponse);

return json;


冉冉说
浏览 100回答 2
2回答

繁花如伊

我认为Reqs应该是Recs:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;class&nbsp;NewRootObject &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List<New_Events_Dataset>&nbsp;Reqs&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}尝试:&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;class&nbsp;NewRootObject &nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;List<New_Events_Dataset>&nbsp;Recs&nbsp;{&nbsp;get;&nbsp;set;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

繁星点点滴滴

将Reqs重命名为Recs并创建类的默认构造函数并实例化 Recs 列表public class NewRootObject{&nbsp; &nbsp; List<New_Events_Dataset> Recs { get; set; }&nbsp; &nbsp; public NewRootObject()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Recs = new List<New_Events_Dataset>();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP