91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

c# javascriptserializer能處理復雜對象嗎

c#
小樊
81
2024-11-28 04:33:35
欄目: 編程語言

是的,C# 的 JavaScriptSerializer 類可以處理復雜對象。它可以序列化和反序列化各種類型的對象,包括自定義類、集合、字典等。但是,需要注意的是,JavaScriptSerializer 類已經過時,不再建議使用。作為替代方案,你可以使用 JsonConvert 類,它是 Newtonsoft.Json 庫的一部分,功能更強大,而且更易于使用。

以下是一個使用 JavaScriptSerializer 處理復雜對象的示例:

using System;
using System.Web.Script.Serialization;

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

class Program
{
    static void Main()
    {
        Person person = new Person
        {
            FirstName = "John",
            LastName = "Doe",
            Address = new Address
            {
                Street = "123 Main St",
                City = "Anytown",
                State = "CA",
                ZipCode = "12345"
            }
        };

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(person);
        Console.WriteLine(json);

        Person deserializedPerson = serializer.Deserialize<Person>(json);
        Console.WriteLine($"FirstName: {deserializedPerson.FirstName}, LastName: {deserializedPerson.LastName}, Address: {deserializedPerson.Address.Street}, {deserializedPerson.Address.City}, {deserializedPerson.Address.State}, {deserializedPerson.Address.ZipCode}");
    }
}

如果你想使用 JsonConvert,首先需要安裝 Newtonsoft.Json 庫。你可以通過 NuGet 包管理器安裝它,或者通過以下命令行安裝:

dotnet add package Newtonsoft.Json

然后,你可以使用以下代碼序列化和反序列化復雜對象:

using System;
using Newtonsoft.Json;

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

class Program
{
    static void Main()
    {
        Person person = new Person
        {
            FirstName = "John",
            LastName = "Doe",
            Address = new Address
            {
                Street = "123 Main St",
                City = "Anytown",
                State = "CA",
                ZipCode = "12345"
            }
        };

        string json = JsonConvert.SerializeObject(person);
        Console.WriteLine(json);

        Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
        Console.WriteLine($"FirstName: {deserializedPerson.FirstName}, LastName: {deserializedPerson.LastName}, Address: {deserializedPerson.Address.Street}, {deserializedPerson.Address.City}, {deserializedPerson.Address.State}, {deserializedPerson.Address.ZipCode}");
    }
}

0
昆山市| 达日县| 安阳县| 阿克| 嘉鱼县| 衡阳县| 信宜市| 红安县| 开封市| 黄平县| 台中县| 丰都县| 苏尼特右旗| 三都| 新乡市| 新沂市| 麻江县| 浙江省| 永仁县| 天长市| 梓潼县| 隆尧县| 金乡县| 卢湾区| 垫江县| 原阳县| 偃师市| 长葛市| 广德县| 桐乡市| 分宜县| 文昌市| 舒城县| 手机| 吴忠市| 织金县| 罗山县| 昌乐县| 兴安盟| 融水| 绩溪县|