C# Json Serialzer

本文介绍了一个使用 C# 实现的对象序列化示例。通过 DataContractJsonSerializer 类将对象数组转换为 JSON 格式字符串,并能将字符串反序列化回原始对象。文章提供了完整的代码实现,包括自定义类 A 的序列化与反序列化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

These two methods are tool methods, they are very useful. Below code's demo is about object array serialisation. So theGetknowTypes method is used in the constructor of the class namedDataContractJsonSerializer

The project demo the serialisation of object.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;


namespace ExampleSerialize
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] obj1 = new[] { "1", "2", "3" };

            string value = SerializeObject<object[]>(obj1);

            Console.WriteLine("Object string: "+value);

            object[] obj2 = DeserializeString<object[]>(value);

            Console.WriteLine("Object: "+obj2);

            object[] obj3 = new[] { new A() };

            ((A)DeserializeString<object[]>(SerializeObject<object[]>(obj3))[0]).Speak();

            Console.ReadLine();
        }

        private static string SerializeObject<T>(T data)
        {
            string serializeString = null;

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(object), GetknowTypes(null));

            MemoryStream str = new MemoryStream();

            serializer.WriteObject(str,data);

            byte[] buffer = new byte[str.Length];

            str.Seek(0,SeekOrigin.Begin);

            str.Read(buffer,0,(int)str.Length);

            serializeString = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

            return serializeString;
        }

        private static T DeserializeString<T>(string dataString)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(object),GetknowTypes(null));

            MemoryStream str = new MemoryStream();

            byte[] buffer = Encoding.UTF8.GetBytes(dataString);

            str.Write(buffer,0,buffer.Length);

            str.Seek(0,SeekOrigin.Begin);

            T result = (T)serializer.ReadObject(str);

            return result;
        }

        private static IEnumerable<Type> GetknowTypes(object provider)
        {
            yield return typeof(object[]);
            yield return typeof(string[]);
            yield return typeof(A);
            yield return typeof(A[]);
        }
    }

    public class A
    {
        public string Name { get; set; }
        public string Words { get; set; }

        public A()
        {
            this.Name = "A";
            this.Words = "I am A";
        }

        public A(string name, string words)
        {
            this.Name = name;
            this.Words = words;
        }

        public void Speak()
        {
            Console.WriteLine(this.Name+": "+this.Words);
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值