Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

How to deserialize an object using Json.NET


In this recipe, we show you how to use Newtonsoft's Json.NET to deserialize JSON to an object that's an instance of a class. We'll use Json.NET, which we mentioned in Chapter 1, Reading and Writing JSON on the Client, because although this works with the existing .NET JSON serializer, there are other things that I want you to know about Json.NET, which we'll discuss in the next two recipes.

Getting ready

To begin, you need to be sure you have a reference to Json.NET in your project. The easiest way to do this is to use NuGet; launch NuGet, search for Json.NET, and click on Install, as shown in the following screenshot:

You'll also need a reference to the Newonsoft.Json namespace in any file that needs those classes with a using directive at the top of your file:

usingNewtonsoft.Json;

How to do it…

Here's an example that provides the implementation of a simple class, converts a JSON string to an instance of that class, and then converts the instance back...