C# JavaScriptSerializer 命名空间问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18746026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScriptSerializer namespace issue
提问by Jeff
I am having a problem trying to implement the JavaScriptSerializer
to parse a JSON string received from a server.
我在尝试实现JavaScriptSerializer
解析从服务器接收到的 JSON 字符串时遇到问题。
I implemented the following code:
我实现了以下代码:
responseFromServer = readStream.ReadLine();
JavaScriptSerializer ser = new JavaScriptSerializer();
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseFromServer);
var status = dict["notificationType"];
Debug.WriteLine(status);
I added using System.Web.Script.Serialization;
我加了 using System.Web.Script.Serialization;
Visual C# 2010 Express is telling me the namespace name Script
does not exist in the namespace System.Web
. As a result the JavaScriptSerializer
is not valid.
Visual C# 2010 Express 告诉我命名空间Script
中不存在命名空间名称System.Web
。结果JavaScriptSerializer
是无效的。
What am I missing to use this, or is there a better way to parse the JSON string?
我缺少什么来使用它,或者有更好的方法来解析 JSON 字符串吗?
采纳答案by Dmitry Dovgopoly
JavaScriptSerializer
is situated in System.Web.Extensions
Assembly. You should add it to your project references.
JavaScriptSerializer
位于System.Web.Extensions
大会。您应该将其添加到您的项目引用中。
You can get this information in MSDN
您可以在MSDN 中获取此信息
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
程序集:System.Web.Extensions(在 System.Web.Extensions.dll 中)
回答by DROP TABLE users
I have had this problem and I added project reference for System.Web.Extensions
, try adding that reference to your project and see if it helps
我遇到了这个问题,我添加了 的项目引用System.Web.Extensions
,尝试将该引用添加到您的项目中,看看它是否有帮助
回答by Lijin Durairaj
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var dict = oSerializer.Deserialize<Dictionary<string, object>>(responseFromServer);
This will help you to get the value
这将帮助您获得价值
回答by Kiot Kiot
install Desharp package you can use this command in package manager console
安装 Desharp 包,您可以在包管理器控制台中使用此命令
Install-Package Desharp -Version 1.2.11
安装包 Desharp - 版本 1.2.11
回答by Md Shahriar
Add Reference System.Web.Extensions
添加参考 System.Web.Extensions
then type using System.Web.Script.Serialization;
然后使用 System.Web.Script.Serialization 键入;
Now you should get JavaScriptSerializer valid.
现在你应该让 JavaScriptSerializer 有效。
You can use better approach by adding Newtonsoft.Json.dll through
您可以通过添加 Newtonsoft.Json.dll 来使用更好的方法
Add Reference. See details here : http://json.codeplex.com/
添加参考。在此处查看详细信息:http: //json.codeplex.com/