gkiko
gkiko

Reputation: 2289

How to convert object to JSON without JSON.NET library?

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?

Upvotes: 7

Views: 21274

Answers (5)

Jilberta
Jilberta

Reputation: 2866

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.

Upvotes: 5

p.s.w.g
p.s.w.g

Reputation: 149020

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.

Upvotes: 5

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

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.

Upvotes: 2

Christopher Painter
Christopher Painter

Reputation: 55581

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.

Upvotes: 6

Xavier Poinas
Xavier Poinas

Reputation: 19733

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

Upvotes: 0

Related Questions