Json.NET (Newtonsoft.Json)

repository·master·Indexed 11 days ago

https://github.com/jamesnk/newtonsoft.json

A high-performance JSON framework for .NET used for serializing and deserializing data between .NET objects and JSON. It features JsonConvert for object conversion, LINQ to JSON via JObject and JArray for dynamic manipulation, and support for various .NET environments including .NET Standard and legacy frameworks.

Tokens
949
Snippets
3
Records
6
Agent score
94%

What's inside Json.NET

  1. Overview of Json.NET

    master

    Json.NET is a high-performance JSON framework for .NET designed for speed and flexibility. It provides several core capabilities:

    • JSON Serialization: Flexible conversion between .NET objects and JSON strings.
    • LINQ to JSON: A way to manually read and write JSON using LINQ-style queries.
    • High Performance: Optimized to be faster than the built-in .NET JSON serializers.
    • Formatting: Support for writing indented, human-readable JSON.
    • XML Interop: Ability to convert JSON to and from XML.
  2. Overview of Json.NET (Newtonsoft.Json)

    master
    Json.NET (also known as Newtonsoft.Json) is a popular JSON framework for .NET. It provides high-performance JSON serialization and deserialization, allowing developers to convert between .NET objects and JSON strings. It is widely used for handling complex data structures, custom formatting, and advanced JSON manipulation in .NET applications.
  3. Select the correct Json.NET version for your .NET environment

    master

    Json.NET provides different library packages depending on your target .NET framework or platform. Choose the package that matches your environment:

    Standard Frameworks

    • net45: .NET 4.5 and above
    • net40: .NET 4.0
    • net35: .NET 3.5
    • net20: .NET 2.0 or Unity

    .NET Standard

    • netstandard1.0
    • netstandard1.3
    • netstandard2.0

    Portable / Mobile / Web

    • portable-net45+win8+wpa81+wp8: .NET 4.5, Windows Phone 8, Windows 8 Store
    • portable-net40+win8+wpa81+wp8+sl5: .NET 4.0, Windows Phone 8, Windows 8 Store, Silverlight 5, MonoTouch, MonoDroid

    Legacy / Specific Requirements

    • For Compact Framework 3.5 and Silverlight 3.0: Use Json.NET 3.5
    • For Silverlight 4.0 and Windows Phone 7: Use Json.NET 5
  4. Deserialize JSON to objects with JsonConvert.DeserializeObject

    master

    Convert a JSON string back into a .NET object using JsonConvert.DeserializeObject<T>. You must specify the target type T within the generic method call.

    string json = @"{ 
      'Name': 'Bad Boys', 
      'ReleaseDate': '1995-4-7T00:00:00', 
      'Genres': [ 
        'Action', 
        'Comedy' 
      ] 
    }";
    
    Movie m = JsonConvert.DeserializeObject<Movie>(json);
    
    string name = m.Name;
    // Bad Boys
  5. Manipulate JSON using LINQ to JSON

    master

    Use JObject and JArray to build and manipulate JSON structures dynamically without needing a predefined class schema. This approach allows for manual construction of JSON trees.

    JArray array = new JArray();
    array.Add("Manual text");
    array.Add(new DateTime(2000, 5, 23));
    
    JObject o = new JObject();
    o["MyArray"] = array;
    
    string json = o.ToString();
    // {
    //   "MyArray": [
    //     "Manual text",
    //     "2000-05-23T00:00:00"
    //   ]
    // }
  6. Serialize objects to JSON with JsonConvert.SerializeObject

    master

    Convert .NET objects into JSON strings using JsonConvert.SerializeObject. This method takes an object and produces a JSON representation of its properties.

    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Sizes = new string[] { "Small" };
    
    string json = JsonConvert.SerializeObject(product);
    // {
    //   "Name": "Apple",
    //   "Expiry": "2008-12-28T00:00:00",
    //   "Sizes": [
    //     "Small"
    //   ]
    // }