在.Net 4.0中找不到JavaScriptSerializer

本文翻译自:Cannot find JavaScriptSerializer in .Net 4.0

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? 我似乎无法在Visual Studio 2010中找到JavaScriptSerializer对象和System.Web.Script.Serialization命名空间。我需要将某些内容序列化为JSON我应该使用什么?

And yes, I already included the System.Web.Extensions (in System.Web.Extensions.dll) within the project. 是的,我已经在项目中包含了System.Web.Extensions (在System.Web.Extensions.dll中)。 Which is why I am shocked? 这让我感到震惊的原因是什么?

  • I do know System.Web.Extensions was marked as obsolete in 3.5 我知道System.Web.Extensions在3.5中被标记为已废弃

#1楼

参考:https://stackoom.com/question/tnej/在-Net-中找不到JavaScriptSerializer


#2楼

This is how to get JavaScriptSerializer available in your application, targetting .NET 4.0 (full) 这是如何在您的应用程序中使用JavaScriptSerializer,目标是.NET 4.0 (完整版)

using System.Web.Script.Serialization;

This should allow you to create a new JavaScriptSerializer object! 这应该允许您创建一个新的JavaScriptSerializer对象!


#3楼

You have to add the reference to the project. 您必须添加对项目的引用。

In Assemblies, there is a System.Web.Extensions Add that. 在Assemblies中,有一个System.Web.Extensions添加它。

Once that is done put: 一旦完成,就把:

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

That worked for me. 这对我有用。


#4楼

  1. Right click References and do Add Reference , then from Assemblies->Framework select System.Web.Extensions . 右键单击References并执行Add Reference ,然后从Assemblies->Framework选择System.Web.Extensions
  2. Now you should be able to add the following to your class file: 现在您应该能够将以下内容添加到您的类文件中:
    using System.Web.Script.Serialization;

#5楼

For those who seem to be following the answers above but still have the problem (eg, 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引用System.Web.Extensions
  • project A references project B 项目A参考项目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. 但是,如果要修改的文件使用System.Web.Script.Serialization在项目B中,那么您还需要在项目B中添加对System.Web.Extension的引用。


#6楼

using System.Web.Script.Serialization; 

在程序集中:System.Web.Extensions(System.Web.Extensions.dll)

你可能感兴趣的:(c#,visual-studio,jsonserializer)