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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3275863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:31:50  来源:igfitidea点击:

Does .NET 4 have a built-in JSON serializer/deserializer?

.netjsonserializationjsonserializer

提问by Cheung

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

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

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

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

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

  • 我在 MSDN 上找到了Data Contracts,但它适用于 WCF,而不适用于 Winforms 或 WPF。

采纳答案by Ben Robinson

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

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

回答by Darin Dimitrov

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

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

回答by vinsa

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

使用这个通用类来序列化/反序列化 JSON。您可以像这样轻松序列化复杂的数据结构:

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

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

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

public class JsonSerializer
{
    public string Serialize<T>(T aObject) where T : new()
    {
        T serializedObj = new T();
        MemoryStream ms = new MemoryStream(); 
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        ser.WriteObject(ms, aObject);
        byte[] json = ms.ToArray();
        ms.Close();
        return Encoding.UTF8.GetString(json, 0, json.Length);
    }

    public T Deserialize<T>(string aJSON) where T : new()
    {
        T deserializedObj = new T();
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(aJSON));
        DataContractJsonSerializer ser = new DataContractJsonSerializer(aJSON.GetType());
        deserializedObj = (T)ser.ReadObject(ms);
        ms.Close();
        return deserializedObj;
    }
}

回答by user30150

.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

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