添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • How to validate email in C#?
  • How to get the sizeof a datatype in C#?
  • Difference between String and StringBuilder in C#
  • How to send emails in C#?
  • Static vs Singleton in C#
  • Difference between == and Equals() Method in C#
  • Asynchronous programming with async, await, Task in C#
  • Difference between static, readonly, and constant in C#
  • IndexOutOfRangeException in C#
  • Foreach Loop in C#
  • How to loop through an enum in C#?
  • NullReferenceException in C#
  • Generate Random Numbers in C#
  • Set Default Value to Property in C#
  • Variable Scopes in C#
  • When to use Struct over Class in C#
  • Difference between Two Dates in C#
  • Convert int to enum in C#
  • BigInteger Data Type in C#
  • Convert String to Enum in C#
  • Convert an Object to JSON in C#
  • Convert JSON String to Object in C#
  • The Main() Method in C#
  • How to Pass or Access Command-line Arguments in C#?
  • DateTime Formats in C#
  • How to convert date object to string in C#?
  • Searching in C# array
  • Compare strings in C#
  • How to count elements in C# array?
  • How to combine two arrays without duplicate values in C#?
  • Difference between String and string in C#.
  • How to get a comma separated string from an array in C#?
  • How to remove duplicate values from an array in C#?
  • How to sort an array in C#?
  • How to sort object array by specific property in C#?
  • ref Keyword in C#
  • Query geolocation & proxy data in .NET using IP2Location
  • Boxing and Unboxing in C#
  • out keyword in C#
  • How to convert string to int in C#?
  • Design Principle vs Design Pattern
  • How to calculate the code execution time in C#?
  • How to read file using StreamReader in C#?
  • Difference between delegates and events in C#
  • How to sort the generic SortedList in the descending order?
  • How to write file using StreamWriter in C#?
  • Difference between Array and ArrayList
  • Difference between Hashtable and Dictionary
  • public class Department{
        public int DeptId { get; set; }
        public string DepartmentName { get; set; }
            The .NET Core 3.0 and later versions include the built-in class JsonSerializer in the System.Text.Json namespace that provides functionality for serializing and deserializing from JSON. 
        The .NET 4.x framework does not provide any built-in JsonSerializer class that converts objects to JSON.
        You have to install the NuGet package Microsoft.Extensions.Configuration.Json in your project to include the System.Text.Json.JsonSerializer to your project which can be used to convert objects to JSON and vice-versa.
            Deserialization is the process of parsing a string into an object of a specific type. The JsonSerializer.Deserialize() method converts a JSON string into an object of the type specified by a generic type parameter.
    

    Syntax:

    public static TValue? Deserialize<TValue> (string json, 
                            JsonSerializerOptions? options = default);
    The following example shows how to parse a JSON string using the JsonSerializer.Deserialize() method:
    Example: Parsing JSON String
    string jsonData = "{\"DeptId\": 101, \"DepartmentName\": \"IT\"}";
    Department deptObj = JsonSerializer.Deserialize<Department>(jsonData);
    Console.WriteLine("Department Id: {0}", deptObj.DeptId);
    Console.WriteLine("Department Name: {0}", deptObj.DepartmentName);
            
                
    Output:
    Department Id is: 101
    Department Name is: IT

    Convert JSON Array String to List

    Many times the JSON string contains an array to store multiple data. This can be converted to an array or list of objects in C#. The following example shows how to parse JSON array to C# list collection.
    Example:
    string jsonArray = "[{\"DeptId\": 101,\"DepartmentName\":\"IT\" }, {\"DeptId\": 102,\"DepartmentName\":\"Accounts\" }]";
    var deptList = JsonSerializer.Deserialize<IList<Department>>(jsonArray);
    foreach(var dept in deptList)
        Console.WriteLine("Department Id is: {0}", dept.DeptId);
        Console.WriteLine("Department Name is: {0}", dept.DepartmentName);
            
        
    Output:
    Department Id is: 101
    Department Name is: IT
    Department Id is: 102
    Department Name is: Accounts

    Convert JSON String to Object in AJAX Application

    Use the JavaScriptSerializer class to provide serialization and deserialization functionality for AJAX-enabled ASP.NET web applications. The JavaScriptSerializer.Deserialize() method converts the specified JSON string to the type of the specified generic parameter object. The following example shows how to parse JSON string using JavaScriptSerializer.Deserialize() method.
    Example:
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.Script.Serialization;
    namespace MyWebApplication
        public partial class _Default : Page
            protected void Page_Load(object sender, EventArgs e)
                string jsonDept = @"{'DeptId': '101', 'DepartmentName': 'IT'}";
                var serializer = new JavaScriptSerializer();
                Department deptObj = new serializer.Deserialize<Department>(jsonDept);
        public class Department
            public int DeptId { get; set; }
            public string DepartmentName { get; set; }
                    
  • How to get the sizeof a datatype in C#?
  • Difference between String and StringBuilder in C#
  • Static vs Singleton in C#
  • Difference between == and Equals() Method in C#
  • Asynchronous programming with async, await, Task in C#
  • How to loop through an enum in C#?
  • Generate Random Numbers in C#
  • Difference between Two Dates in C#
  • Convert int to enum in C#
  • BigInteger Data Type in C#
  • Convert String to Enum in C#
  • Convert an Object to JSON in C#
  • DateTime Formats in C#
  • How to convert date object to string in C#?
  • Compare strings in C#
  • How to count elements in C# array?
  • Difference between String and string in C#.
  • How to get a comma separated string from an array in C#?
  • Boxing and Unboxing in C#
  • How to convert string to int in C#?
  • TutorialsTeacher.com is optimized for learning web technologies step by step. Examples might be simplified to improve reading and basic understanding. While using this site, you agree to have read and accepted our terms of use and privacy policy.