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 09:55
using System.Web.Script.Serialization; 

is in assembly : System.Web.Extensions (System.Web.Extensions.dll)

查看更多
Explosion°爆炸
3楼-- · 2020-01-27 09:55

Did you include a reference to System.Web.Extensions? If you click on your first link it says which assembly it's in.

查看更多
forever°为你锁心
4楼-- · 2020-01-27 09:56
  1. Right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.
  2. Now you should be able to add the following to your class file:
    using System.Web.Script.Serialization;
查看更多
女痞
5楼-- · 2020-01-27 09:56

For those who seem to be following the answers above but still have the problem (e.g., see the first comment on the poster's question):

You are probably working in a solution with many projects. The project you appear to be working in references other projects, but you are actually modifying a file from one of the other projects. For example:

  • project A references System.Web.Extensions
  • project A references project B

But if the file you are modifying to use System.Web.Script.Serialization is in project B, then you will need to add a reference to System.Web.Extension in project B as well.

查看更多
Luminary・发光体
6楼-- · 2020-01-27 10:03

Check if you included the .net 4 version of System.Web.Extensions - there's a 3.5 version as well, but I don't think that one works.

These steps work for me:

  1. Create a new console application
  2. Change the target to .net 4 instead of Client Profile
  3. Add a reference to System.Web.Extensions (4.0)
  4. Have access to JavaScriptSerializer in Program.cs now :-)
查看更多
唯我独甜
7楼-- · 2020-01-27 10:05

Just so you know, I am using Visual Studio 2013 and have had the same problem until I used the Project Properties to switch to 3.5 framework and back to 4.5. This for some reason registered the .dll properly and I could use the System.Web.Extensions.

enter image description here

enter image description here

查看更多
登录 后发表回答