关于C#对Json进行序列化和反序列化的方法

时间:2025-03-27 14:32:21

public static class Serialization

{

    public static T JsonToObject<T>(string jsonText)

    {

        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));

        MemoryStream memoryStream = new MemoryStream(Encoding.(jsonText));

        T result = (T)((object)(memoryStream));

        ();

        return result;

    }

    public static string ObjectToJSON<T>(T obj)

    {

        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));

        string result = string.Empty;

        using (MemoryStream memoryStream = new MemoryStream())

        {

            (memoryStream, obj);

             = 0L;

            using (StreamReader streamReader = new StreamReader(memoryStream))

            {

                result = ();

            }

        }

        return result;

    }

}