zoukankan      html  css  js  c++  java
  • C# 使用Json.NET对数据进行序列化和反序列化 | c# json serialize and deserialize using json.net JsonConvert

    本文首发于个人博客https://kezunlin.me/post/22391aa3/,欢迎阅读最新内容!

    c# json serialize and deserialize using json.net JsonConvert

    Guide

    Json.NET

    • JsonConvert.SerializeObject
    • JsonConvert.DeserializeObject

    install

    Use NuGet to download the package

    "Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install".
    

    code

    reference

    using Newtonsoft.Json;
    

    serialize collections

    Product p1 = new Product
    {
        Name = "Product 1",
        Price = 99.95m,
        ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
    };
    Product p2 = new Product
    {
        Name = "Product 2",
        Price = 12.50m,
        ExpiryDate = new DateTime(2009, 7, 31, 0, 0, 0, DateTimeKind.Utc),
    };
    
    List<Product> products = new List<Product>();
    products.Add(p1);
    products.Add(p2);
    
    string json = JsonConvert.SerializeObject(products, Formatting.Indented);
    //[
    //  {
    //    "Name": "Product 1",
    //    "ExpiryDate": "2000-12-29T00:00:00Z",
    //    "Price": 99.95,
    //    "Sizes": null
    //  },
    //  {
    //    "Name": "Product 2",
    //    "ExpiryDate": "2009-07-31T00:00:00Z",
    //    "Price": 12.50,
    //    "Sizes": null
    //  }
    //]
    

    deserialize collections

    string json = @"[
      {
        'Name': 'Product 1',
        'ExpiryDate': '2000-12-29T00:00Z',
        'Price': 99.95,
        'Sizes': null
      },
      {
        'Name': 'Product 2',
        'ExpiryDate': '2009-07-31T00:00Z',
        'Price': 12.50,
        'Sizes': null
      }
    ]";
    
    List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);
    
    Console.WriteLine(products.Count);
    // 2
    
    Product p1 = products[0];
    
    Console.WriteLine(p1.Name);
    // Product 1
    

    serialize to json file

    public class Movie
    {
        public string Name { get; set; }
        public int Year { get; set; }
    }
    
    
    Movie movie = new Movie
    {
        Name = "Bad Boys",
        Year = 1995
    };
    
    // serialize JSON to a string and then write string to a file
    File.WriteAllText(@"c:movie.json", JsonConvert.SerializeObject(movie));
    
    // serialize JSON directly to a file
    using (StreamWriter file = File.CreateText(@"c:movie.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        serializer.Serialize(file, movie);
    }
    

    Reference

    History

    • 20190910: created.

    Copyright

  • 相关阅读:
    C#中char[]与string之间的转换
    Java文件操作之文件追加 Chars
    冒泡排序法的改进 Chars
    选择排序 Chars
    编译原理 Chars
    Apk反编译 Chars
    VC中常用的方法 Chars
    Node.Js入门级《培训》
    新概念系列之《Part2 Lesson 24 It could be worse》
    《CLR via C#》Part2之Chapter5 基元类型、引用类型和值类型(三)
  • 原文地址:https://www.cnblogs.com/kezunlin/p/11975856.html
Copyright © 2011-2022 走看看