Click or drag to resize
Json.NETSerialize JSON to a file
 

This sample serializes JSON to a file.

Sample
Types
1public class Movie
2{
3    public string Name { get; set; }
4    public int Year { get; set; }
5}
Usage
 1Movie movie = new Movie
 2{
 3    Name = "Bad Boys",
 4    Year = 1995
 5};
 6
 7// serialize JSON to a string and then write string to a file
 8File.WriteAllText(@"c:\movie.json", JsonConvert.SerializeObject(movie));
 9
10// serialize JSON directly to a file
11using (StreamWriter file = File.CreateText(@"c:\movie.json"))
12{
13    JsonSerializer serializer = new JsonSerializer();
14    serializer.Serialize(file, movie);
15}