简体   繁体   中英

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?

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.

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM