C# 的 JSON 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1474377/
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
JSON library for C#
提问by weilin8
Does Microsoft provide any library to work with JSON in C#? If not, what open source library do you recommend?
Microsoft 是否提供任何库来处理 C# 中的 JSON?如果没有,你推荐什么开源库?
回答by slolife
回答by Hannoun Yassir
Have a look at the system.web.script.serialization namespace (i think you will need .Net 3.5)
看看 system.web.script.serialization 命名空间(我认为你需要 .Net 3.5)
回答by James Black
If you look here, you will see several different libraries for JSON on C#.
如果您查看此处,您将在 C# 上看到几个不同的 JSON 库。
You will find a version for LINQ as well as some others. There are about 7 libraries for C# and JSON.
您将找到 LINQ 和其他一些版本。C# 和 JSON 大约有 7 个库。
回答by Ty.
The .net framework supports JSON through JavaScriptSerializer. Here is a good example to get you started.
.net 框架通过 JavaScriptSerializer 支持 JSON。这是一个很好的例子,可以帮助您入门。
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace GoogleTranslator.GoogleJSON
{
public class FooTest
{
public void Test()
{
const string json = @"{
""DisplayFieldName"" : ""ObjectName"",
""FieldAliases"" : {
""ObjectName"" : ""ObjectName"",
""ObjectType"" : ""ObjectType""
},
""PositionType"" : ""Point"",
""Reference"" : {
""Id"" : 1111
},
""Objects"" : [
{
""Attributes"" : {
""ObjectName"" : ""test name"",
""ObjectType"" : ""test type""
},
""Position"" :
{
""X"" : 5,
""Y"" : 7
}
}
]
}";
var ser = new JavaScriptSerializer();
ser.Deserialize<Foo>(json);
}
}
public class Foo
{
public Foo() { Objects = new List<SubObject>(); }
public string DisplayFieldName { get; set; }
public NameTypePair FieldAliases { get; set; }
public PositionType PositionType { get; set; }
public Ref Reference { get; set; }
public List<SubObject> Objects { get; set; }
}
public class NameTypePair
{
public string ObjectName { get; set; }
public string ObjectType { get; set; }
}
public enum PositionType { None, Point }
public class Ref
{
public int Id { get; set; }
}
public class SubObject
{
public NameTypePair Attributes { get; set; }
public Position Position { get; set; }
}
public class Position
{
public int X { get; set; }
public int Y { get; set; }
}
}
回答by Bjorn Bailleul
Try the Vici Project, Vici Parser. It includes a JSON parser / tokeniser. It works great, we use it together with the MVC framework.
试试 Vici 项目,Vici Parser。它包括一个 JSON 解析器/标记器。它工作得很好,我们将它与 MVC 框架一起使用。
More info at: http://viciproject.com/wiki/projects/parser/home
更多信息请访问:http: //viciproject.com/wiki/projects/parser/home
I forgot to say that it is open source so you can always take a look at the code if you like.
我忘了说它是开源的,所以你可以随时查看代码。
回答by mythz
You should also try my ServiceStack JsonSerializer- it's the fastest .NET JSON serializer at the moment based on the benchmarks of the leading JSON serializersand supports serializing any POCO Type, DataContracts, Lists/Dictionaries, Interfaces, Inheritance, Late-bound objects including anonymous types, etc.
您还应该尝试我的 ServiceStack JsonSerializer- 它是目前最快的 .NET JSON 序列化器,基于领先的 JSON 序列化器的基准测试,并支持序列化任何 POCO 类型、数据合同、列表/字典、接口、继承、后期绑定对象,包括匿名类型等。
Basic Example
基本示例
var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = customer.ToJson();
var fromJson = json.FromJson<Customer>();
Note: Only use Microsofts JavaScriptSerializer if performance is not important to you as I've had to leave it out of my benchmarks since its up to 40x-100xslower than the other JSON serializers.
注意:只有在性能对您不重要的情况下才使用 Microsoft 的 JavaScriptSerializer,因为我不得不将它排除在我的基准测试之外,因为它比其他 JSON 序列化程序慢40 到 100 倍。
回答by Maggie Ying
To answer your first question, Microsoft does ship a DataContractJsonSerializer: see msdn How to: Serialize and Deserialize JSON Data
要回答您的第一个问题,Microsoft 确实提供了 DataContractJsonSerializer:请参阅 msdn How to: Serialize and Deserialize JSON Data
回答by Pejvan
To give a more up to date answer to this question: yes, .Net includes JSON seriliazer/deserliazer since version 3.5 through the System.Runtime.Serialization.Json Namespace: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json(v=vs.110).aspx
要对此问题提供最新答案:是的,.Net 从 3.5 版开始通过 System.Runtime.Serialization.Json 命名空间包含 JSON seriliazer/deserliazer:http: //msdn.microsoft.com/en-us/library /system.runtime.serialization.json(v=vs.110).aspx
But according to the creator of JSON.Net, the .Net Framework compared to his open source implementation is very much slower.
但是根据 JSON.Net 的创建者的说法,.Net Framework 与他的开源实现相比要慢得多。