如何在C#中执行System.Web.Script.Serialization?

时间:2013-07-15 05:50:04

标签: c# json javascriptserializer system.web downloadstring

如何在C#modern UI中执行此操作?

var url = "http://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fwww.digg.com%2Frss%2Findex.xml&v=1.0";
var wc = new WebClient();
var rawFeedData = wc.DownloadString(url);

//You can use System.Web.Script.Serialization if you don't want to use Json.NET
JavaScriptSerializer ser = new JavaScriptSerializer();
FeedApiResult foo = ser.Deserialize<FeedApiResult>(rawFeedData);

//Json.NET also return you the same strong typed object     
var apiResult = JsonConvert.DeserializeObject<FeedApiResult>(rawFeedData);

它在WebClient和System.Web.Script.Serialization

中给出了错误

2 个答案:

答案 0 :(得分:55)

我找到了如何使它发挥作用 我不知道为什么他们这么乱,我甚至不明白为什么,但这里是解决方案: 右键单击项目并添加对:

的引用

System.Web.Extensions程序

比您可以添加所需的参考:

using System.Web.Script.Serialization;

这次它会起作用。 (至少这是我在.NET framework 3.5上所做的)

答案 1 :(得分:0)

我发现 System.Web.Script.Serialization 在 System.Web.Extensions