有没有现成的,现有的JSON序列化帮手.NET基础类库类?帮手、类库、基础、序列化

2023-09-03 00:21:42 作者:幸福加载中…

我需要序列化/从字符串反序列化一些对象为/,转让刚才不透明的数据。我可以用XmlSerializer的做,但生成的字符串看起来笨拙而长。那么,有没有简明的序列化提供.NET?

I need to serialize/de-serialize some objects into/from string and transfer them as just opaque data. I can use XmlSerializer to do it, but generated string looks clumsy and long. So, is there any concise Serializer available in .NET?

第一件事来我想到的是,也许是.NET应该有JSON序列化,但我不能找到它。有没有现成的现成办法在.NET中对象转换为/从简洁的字符串?

The first thing coming to my mind is that perhaps .NET should have JSON Serializer, but I cannot find it. Is there any off-the-shelf approach in .NET to convert object to/from concise string?

推荐答案

有两种;

DataContractJsonSerializer 3.5 JavaScriptSerializer在3.5 SP1 DataContractJsonSerializer in 3.5 JavaScriptSerializer in 3.5 SP1

在传统的方式,它们是相互,其他不是100%兼容,尤其是重的DateTime ; DCJS用途(IIRC)一个文本字符串; JSS使用 - 既不可以正确读取t'other

In the traditional way, they aren't 100% compatible with each-other, especially re DateTime; DCJS uses (IIRC) a literal string; JSS uses new - and neither can read t'other correctly.

当然,如果文本,如果不透明,你也可以使用任何简明的二进制序列,并简单地基地-64连接code它;例如, protobuf网是pretty的简洁。但是,使用JSON最大限度地减少所需的各端的外部code,那么很可能是你最好的选择。

Of course, if the text if opaque, you could also use any concise binary serializer, and simply base-64 encode it; for example, protobuf-net is pretty terse. But using JSON minimizes the external code needed at each end, so may well be your best bet.