Cannot find JavaScriptSerializer in .Net 4.0

2020-01-27 09:33发布

I cannot seem to find the JavaScriptSerializer object nor the the System.Web.Script.Serialization namespace within Visual Studio 2010. I need to serialize something to JSON what am I supposed to use?

And yes, I already included the System.Web.Extensions (in System.Web.Extensions.dll) within the project. Which is why I am shocked?

  • I do know System.Web.Extensions was marked as obsolete in 3.5

12条回答
对你真心纯属浪费
2楼-- · 2020-01-27 10:07

This is how to get JavaScriptSerializer available in your application, targetting .NET 4.0 (full)

using System.Web.Script.Serialization;

This should allow you to create a new JavaScriptSerializer object!

查看更多
smile是对你的礼貌
3楼-- · 2020-01-27 10:10

Are you targeting the .NET 4 framework or the .NET 4 Client Profile?

If you're targeting the latter, you won't find that class. You also may be missing a reference, likely to an extensions dll.

查看更多
不美不萌又怎样
4楼-- · 2020-01-27 10:10

You have to add the reference to the project.

In Assemblies, there is a System.Web.Extensions Add that.

Once that is done put:

    using System.Web;
    using System.Web.Script;
    using System.Web.Script.Serialization;

That worked for me.

查看更多
ら.Afraid
5楼-- · 2020-01-27 10:13

You can use another option which is the Newtonsoft.Json, you can install it from NuGet Package Manager.

Tools >> Nuget Package Manager >> Package Manager Console by issuing command

Install-Package Newtonsoft.Json

or

by using the GUI at Tools >> Nuget Package Manager >> Manage NuGet Packages for Solution...

enter image description here

查看更多
三岁会撩人
6楼-- · 2020-01-27 10:18

I'm using Visual Studio 2015 and finally ran across this post.

Yes in order to use

JavaScriptSerializer json = new JavaScriptSerializer();

You must right click on references and under Assemblies --> Framework choose

System.Web.Extensions

Then add in your reference

using System.Web.Script.Serialization;
查看更多
霸刀☆藐视天下
7楼-- · 2020-01-27 10:19

From the first search result on google:

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

JavaScriptSerializer Class

Provides serialization and deserialization functionality for AJAX-enabled applications.

Inheritance Hierarchy

System.Object

System.Web.Script.Serialization.JavaScriptSerializer

Namespace: System.Web.Script.Serialization

Assembly: System.Web.Extensions (in System.Web.Extensions.dll)

So, include System.Web.Extensions.dll as a reference.

查看更多
登录 后发表回答