Json does not exist in the namespace System

2019-01-14 16:51发布

问题:

In this tutorial: http://www.asp.net/web-api/videos/getting-started/custom-validation Jon uses

dynamic error = new JsonObject();

with

using System.Json;

I guess it's the JsonObject here: http://msdn.microsoft.com/en-us/library/system.json.jsonobject(v=vs.110).aspx located in:

  • Namespace: System.Json
  • Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)

I've added System.Runtime.Serialization a reference but still cannot find System.Json.

Am I reading the Microsoft docs wrong? (I'm using .NET 4.5)

回答1:

Try this:

PM> Install-Package System.Json -Version 4.0.20126.16343

Per: http://nuget.org/packages/System.Json

It worked!

If you have questions on how to add enter nuget code, please follow the link below: http://docs.nuget.org/docs/start-here/using-the-package-manager-console



回答2:

http://www.webcosmoforums.com/asp/32551-type-namespace-name-json-does-not-exist-namespace-system-runtime-serialization.html

Most likely you are missing a reference to System.ServiceModel.Web

Make sure your application is targeting the .Net 4.5 framework in the project properties.

The System.Json objects are only available in 4.5

Edit:

Use Nuget to install system.json : 'Install-Package System.Json'

How to parse JSON without JSON.NET library?



回答3:

The Json object works only from Controller class and not outside. Even if we refer System.Web.MVC outside controller, we have access only to JsonResult and not to Json as Json object is protected object of JsonResult. Please refer the below documentation which explains that,

http://msdn.microsoft.com/en-us/library/dd504936(v=vs.118).aspx



回答4:

If you want to use other .Net json serializer, you can use the following:

  1. go to manage nuget package.
  2. search json.net.
  3. click install.

for more details, follow - http://netfx.codeplex.com/



回答5:

Using System.json will not work for .Net 4 framework. The library is deprecated. To check it try:

  1. Go Add reference... to project.
  2. On .Net Tab, Search System.json, you will not find any.

That means, it is deprecated.