7

I want to save this object:

Student s = new Student();

to Json file. But Visual Studio 2012 can't find none of these namespaces:

System.Web.Script;
System.Json;
System.Runtime.Serialization.Json;

Any idea?

gkiko
  • 2,283
  • 3
  • 30
  • 50
  • Yeah, I've found that you can only install System.Json by installing another package, then deleting it. (Don't recall which package it is.) I get the impression that System.Json is not really supported. – Hot Licks Jun 04 '14 at 00:20
  • Possible answer https://stackoverflow.com/a/70454281/3057246 – Vinod Srivastav Apr 03 '23 at 14:09

5 Answers5

6

Step 1: Google "System.Runtime.Serialization.Json Namespace" and find this page:

System.Runtime.Serialization.Json Namespace

Step 2: Click on the class you are interested in ( let's say JsonReaderWriterFactory Class )

Step 3: Read the part that says what assembly that class is in:

Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)

Step 4: Add that DLL as a reference to your project. See: How to: Add or Remove References By Using the Add Reference Dialog Box

Step 5: Repeat Steps 1 - 4 as needed.

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
5

Make sure you add the appropriate references to your project. For example, to use the datacontractjsonserializer class, you need to add a reference to System.Runtime.Serialization to your project.

If you're using visual studio, read How to: Add or Remove References in Visual Studio.

You can then use it with:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Student));
ser.WriteObject(outputStream, student);

Of course there are many other ways to do this and I won't go into detail about each one (I'm sure you've found some examples since you listed those namespaces).

TIP: if you want to use a .NET class and you're not sure what reference you need to add to your project in order to use it, open up the MSDN page for the class and look for the text like:

Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)

That's the assembly reference you need to add to your project.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
5

If you want to serialize class into Json you can try this one too:

Install JSON.NET if you haven't yet.

make sure to include using Newtonsoft.Json;

and you can try this code:

Student s = new Student();
string json = JsonConvert.SerializeObject(s);

sorry for my bad english.

gkiko
  • 2,283
  • 3
  • 30
  • 50
Jilberta
  • 2,836
  • 5
  • 30
  • 44
  • 5
    This answer is exactly not what the OP wanted and creates click-bait deception search results. – Ryan Naccarato Feb 27 '21 at 19:15
  • The question was **How to convert object to JSON without JSON.NET library ?** and you are telling how to use `Newtonsoft.Json` and he accepts it as answer. I don't know what is happening here. – Vinod Srivastav Apr 03 '23 at 12:27
2

The other answers are correct in saying that you need to add references to the appropriate assemblies. As an easy way to manage references for a personal project, I'd recommend NuGet, a package manager for Visual Studio. Thankfully, NuGet ships with Visual Studio 2012, so you just have to right-click the project, click on "Manage NuGet packages", and search for Json.NET. The first blog post I found on using NuGet in Visual Studio 2012 is here, and it seems to give a nice set of instructions, complete with screenshots.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • The dumbing down of developers... I'd really hope the OP gets a clue on what all this is before turning on the autopilot and letting some tool do it for him. – Christopher Painter Mar 28 '13 at 23:44
  • I'm still largely undecided about Nuget; I often see it as mess of _another_ set of references and can simply do without it in my working environment, gladly even - although I keep trying to drink the kool aid and try to use it on personal machines, maybe the penny will drop and I'll 'get it'. – Grant Thomas Mar 28 '13 at 23:48
  • I have a big concern with NuGet. Hang with me for a moment... see, I'm a deployment expert. (Yes, laugh all you want, I've heard the snarky "deployment expert? bwahaha" comments here before. ) Far too often developers come to me without having a clue what their dependencies are. Understanding dependencies is the first step in coming up with requirements. NuGet automates setting up the development environment and the build environment but does nothing for deployment. My other concerns are it's a bit of a CM nightmare. Otherwise I know what they are trying to do. – Christopher Painter Mar 28 '13 at 23:51
  • @ChristopherPainter Maybe. Rather than see a new programmer say "this C# thing is hard, I'll write everything in PHP," I'd prefer to let him/her use use a package manager for a while, and learn about assembly references when looking for a particular version of the libary, or when using a libary that isn't available from the package manager, or when splitting a project into separate assemblies, or when writing *any* C++ code, or code for Linux in a college course. – Adam Mihalcin Mar 28 '13 at 23:51
  • Otherwise I know what they are trying to do. They are trying to be cool and sexy to all the new freshouts who are used to the way other open source stacks work. – Christopher Painter Mar 28 '13 at 23:52
  • I'm all for making things easier (ReSharper would help here also!) but not at the expense of foundational skills. Just my opinion. – Christopher Painter Mar 28 '13 at 23:58
  • @AdamMihalcin I've installed NuGet. What's the next step? The compiler still can't find the library. – gkiko Mar 29 '13 at 00:40
  • As I stated above, NuGet cannot find System.Json. – Hot Licks Jun 04 '14 at 00:21
0

You probably need to add references to your project:

  • System.Runtime.Serialization.Json is in System.Runtime.Serialization
  • System.Web.Script is in System.Web.Extensions
  • System.Json is only for Silverlight
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95