Serializing and Deserializing JSON (2024)

Serializing and Deserializing JSON (1)

The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values for you.

  • JsonConvert
  • JsonSerializer

Serializing and Deserializing JSON (2)JsonConvert

For simple scenarios where you want to convert to and from a JSON string, the SerializeObject and DeserializeObject methods on JsonConvert provide an easy-to-use wrapper over JsonSerializer.

Serializing and Deserializing JSON with JsonConvert

Copy

Product product = new Product();product.Name = "Apple";product.ExpiryDate = new DateTime(2008, 12, 28);product.Price = 3.99M;product.Sizes = new string[] { "Small", "Medium", "Large" };string output = JsonConvert.SerializeObject(product);//{// "Name": "Apple",// "ExpiryDate": "2008-12-28T00:00:00",// "Price": 3.99,// "Sizes": [// "Small",// "Medium",// "Large"// ]//}Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object.JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.

Serializing and Deserializing JSON (3)JsonSerializer

For more control over how an object is serialized, the JsonSerializer can be used directly.The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReaderand JsonTextWriter.Other kinds of JsonWriters can also be used, such asJTokenReader/JTokenWriter,to convert your object to and from LINQ to JSON objects, orBsonReader/BsonWriter, to convert to and from BSON.

Serializing JSON to a Stream with JsonSerializer

Copy

Product product = new Product();product.ExpiryDate = new DateTime(2008, 12, 28);JsonSerializer serializer = new JsonSerializer();serializer.Converters.Add(new JavaScriptDateTimeConverter());serializer.NullValueHandling = NullValueHandling.Ignore;using (StreamWriter sw = new StreamWriter(@"c:\json.txt"))using (JsonWriter writer = new JsonTextWriter(sw)){ serializer.Serialize(writer, product); // {"ExpiryDate":new Date(1230375600000),"Price":0}}

JsonSerializer has a number of properties on it to customize how it serializes JSON. These can also be used with the methods on JsonConvert via the JsonSerializerSettings overloads.

You can read more about the available JsonSerializer settings here: Serialization Settings

Serializing and Deserializing JSON (4)See Also

Serializing and Deserializing JSON (2024)
Top Articles
Trusted Guidance and Protection | New York Life Insurance
What to Prioritize: Debt Payoff or Saving? - Diana on a Dime
SZA: Weinen und töten und alles dazwischen
Ron Martin Realty Cam
Washu Parking
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Walgreens Pharmqcy
Mcgeorge Academic Calendar
Terrorist Usually Avoid Tourist Locations
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
Horoscopes and Astrology by Yasmin Boland - Yahoo Lifestyle
Boggle Brain Busters Bonus Answers
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Dr Doe's Chemistry Quiz Answer Key
The Pope's Exorcist Showtimes Near Cinemark Hollywood Movies 20
Over70Dating Login
Zendaya Boob Job
Https://Gw.mybeacon.its.state.nc.us/App
Shemal Cartoon
Flower Mound Clavicle Trauma
Spartanburg County Detention Facility - Annex I
Chile Crunch Original
Colts Snap Counts
Interactive Maps: States where guns are sold online most
Buy PoE 2 Chaos Orbs - Cheap Orbs For Sale | Epiccarry
Yakimacraigslist
Craigslist Pet Phoenix
Nearest Walgreens Or Cvs Near Me
Hdmovie2 Sbs
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
No Limit Telegram Channel
Royalfh Obituaries Home
Weather October 15
4.231 Rounded To The Nearest Hundred
Kuttymovies. Com
Ice Dodo Unblocked 76
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Craigs List Tallahassee
Math Minor Umn
Autotrader Bmw X5
Hattie Bartons Brownie Recipe
Bridger Park Community Garden
Kvoa Tv Schedule
Edict Of Force Poe
Сталь aisi 310s российский аналог
Subdomain Finder
Busted Newspaper Mcpherson Kansas
Memberweb Bw
Southwest Airlines Departures Atlanta
Inside the Bestselling Medical Mystery 'Hidden Valley Road'
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6072

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.