.NET 4 是否有内置的 JSON 序列化器/反序列化器?

发布于 2024-09-10 12:29:29 字数 350 浏览 2 评论 0原文

.NET 4 是否附带任何可以序列化/反序列化 JSON 数据的类?

  • 我知道有第 3 方库,例如 JSON.NET ,但我正在寻找直接内置于 .NET 中的东西。

  • 我在 MSDN 上找到了数据契约,但它适用于 WCF ,不适用于 Winforms 或 WPF。

Does .NET 4 come with any class that serializes/deserializes JSON data?

  • I know there are 3rd-party libraries, such as JSON.NET, but I am looking for something built right into .NET.

  • I found Data Contracts on MSDN, but it is for WCF, not for Winforms or WPF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你与昨日 2024-09-17 12:29:29

您可以在任何地方使用 DataContractJsonSerializer 类你想要的,它只是一个.net类,并不限于WCF。有关如何使用它的更多信息,请访问此处此处

You can use the DataContractJsonSerializer class anywhere you want, it is just a .net class and is not limited to WCF. More info on how to use it here and here.

烟花肆意 2024-09-17 12:29:29

JavaScriptSerializer 类(尽管您需要引用 System.Web.Extensions 程序集(该类在 WinForms/WPF 应用程序中工作得很好)。此外,即使 DataContractJsonSerializer 类是专为 WCF 设计,它在客户端应用程序中运行良好。

There's the JavaScriptSerializer class (although you will need to reference the System.Web.Extensions assembly the class works perfectly fine in WinForms/WPF applications). Also even if the DataContractJsonSerializer class was designed for WCF it works fine in client applications.

感性 2024-09-17 12:29:29

使用此通用类来序列化/反序列化 JSON。
您可以轻松序列化复杂的数据结构,如下所示:

Dictionary<string, Tuple<int, int[], bool, string>>

到 JSON 字符串,然后将其保存在应用程序设置中或其他

public class JsonSerializer
{
    public string Serialize<T>(T Obj)
    {
        using (var ms = new MemoryStream())
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            serialiser.WriteObject(ms, Obj);
            byte[] json = ms.ToArray();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
    }

    public T Deserialize<T>(string Json)
    {
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            var deserializedObj = (T)serialiser.ReadObject(ms);
            return deserializedObj;
        }
    }
}

Use this generic class in order to serialize / deserialize JSON.
You can easy serialize complex data structure like this:

Dictionary<string, Tuple<int, int[], bool, string>>

to JSON string and then to save it in application setting or else

public class JsonSerializer
{
    public string Serialize<T>(T Obj)
    {
        using (var ms = new MemoryStream())
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            serialiser.WriteObject(ms, Obj);
            byte[] json = ms.ToArray();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
    }

    public T Deserialize<T>(string Json)
    {
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            var deserializedObj = (T)serialiser.ReadObject(ms);
            return deserializedObj;
        }
    }
}
破晓 2024-09-17 12:29:29

.NET4有一个内置的JSON类,例如 DataContractJsonSerializer ,但它很弱,它不支持多维数组。我建议你使用 JSON.Net

.NET4 has a built-in JSON Class,such as DataContractJsonSerializer ,but it is very weak,it doesn't support multidimentional array. I suggest you use JSON.Net

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文