32

I want to use an API to get info from the interwebz. The API returns data in Json format.

  1. I'm running Microsoft Visual Studio C# 2010 Express addition.
  2. It appears that I have the .NET Framework 4 Client Profile set as my "Target framework" but I'm honestly not sure exactly what this means.
  3. This is a Windows Forms Application...

Not much code to show because I can't really get started without the appropriate using statement...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;
using System.Runtime.Serialization.Json;

I get this error:

The type or namespace name 'Json' does not exist in the namespace 'System.Runtime.Serialization' (are you missing an assembly reference?)

Am I missing a DLL file or something? Based on my hours of fruitlessly searching for solutions, I understand that the .NET 4.xx should already have the tools needed to parse up a Json formatted string?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Methodician
  • 2,396
  • 5
  • 30
  • 49
  • 2
    See here http://stackoverflow.com/questions/2682147/where-is-the-system-runtime-serialization-json-namespace – acbod Mar 07 '14 at 16:40

6 Answers6

63

The System.Runtime.Serialization.Json Namespace is in two different DLL's depending on your .net framework.

In .NET 3.5 It is in System.ServiceModel.Web.dll

In .NET 4.0 and above It is in System.Runtime.Serialization.dll.

Make sure you have added the correct DLL as a reference in your project and add using System.Runtime.Serialization.Json; to the top of your code file.

EDIT - Consider using JSON.NET

Even though the .NET Framework supplies its own JSON Serialization and Deserialization namespaces (DataContractJsonSerializer and JavaScriptSerializer) you should investigate whether you would be better off using JSON.NET.

JSON.NET is easier to use, better performance and has far more features. http://www.newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm

CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • @CathIMF - I am facing similar issue in .NET 2.0, which dll I should add? – RSB Sep 20 '16 at 13:52
  • 1
    @RSB Those namespaces are not available in .NET 2.0. You need to use a 3rd party library such as JSON.NET. There is a link to it in this answer. – CathalMF Sep 20 '16 at 13:54
  • @CathalMF Thanks for the quick response. – RSB Sep 20 '16 at 13:59
  • As of 2021 I would consider adding `System.Text.Json NuGet` package to this answer, as it is far more downloaded than JSON.NET and authored by Microsoft. Needs .NET version to be above 4.6.1. – Aritz Apr 06 '21 at 09:33
13

you need to import System.Runtime.Serialization dll from reference

Eanthmue
  • 471
  • 2
  • 5
9

You need to add a reference to your project.

In the Solution Explorer right click references then add reference. You'll see a list of DLL's and you have to check the box next to the one you need for it to be added to the project. After you've done this you can successfully add the using statement.

Hope that helps!

eddie_cat
  • 2,527
  • 4
  • 25
  • 43
4

The general process for serializing and deserializing JSON from C# is:

Add a reference to the System.Runtime.Serialization library.

Add using directives for System.Runtime.Serialization and System.Runtime.Serialization.Json.

Jawier
  • 41
  • 1
1

Please change your Target framework from .NET Framework 4 Client Profile to .NET Framework 4

Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
0

I know this is an old question, but I came across this in .NET 5.0 and the solution is to add using System.Text.Json to the top of your code.

ColorCodin
  • 101
  • 2
  • 11