简体   繁体   English

C#是否具有用于将JSON文本转换为可管理对象的内置对象,还是我需要第三方库?

[英]Does C# have any built in objects for turning JSON text into manageable objects or do I need a third party library?

I have all this JSON text that I want to deserialize (or something) into an object with variables so I can run through it and add/change some things and then serialize it back to text. 我已经将所有这些JSON文本都想反序列化(或某种形式)到带有变量的对象中,这样我就可以运行它并添加/更改某些内容,然后将其序列化回文本。 Is there something built in from Microsoft for this? 微软为此内置了什么吗?

WCF has the DataContractJsonSerializer but I haven't used it myself - I've always gone for Json.NET which I've found to be broadly excellent. WCF具有DataContractJsonSerializer但我自己还没有使用过-我一直都选择Json.NET ,我发现它非常出色。

I'd expect the WCF serializer to be a good fit if you're using WCF, but if you're writing a standalone app, I'd go for Json.NET. 如果您使用的是WCF,我希望WCF序列化程序非常合适,但是如果您正在编写一个独立的应用程序,那么我会选择Json.NET。 It's a pretty straightforward dependency. 这是一个非常简单的依赖项。 In particular, I like the fact that I don't actually have to model the classes directly in order to use Json.NET - I tend to use the "LINQ" side of the library which is a little like LINQ to XML, but applied to JSON. 特别地,我喜欢这样一个事实,我实际上不必直接建模即可使用Json.NET。我倾向于使用库的“ LINQ”端,这有点像LINQ to XML,但是可以应用JSON。 I deal directly in JArray , JObject , JToken etc and let Json.NET just do the parsing/formatting. 我直接处理JArrayJObjectJToken等,然后让Json.NET进行解析/格式化。

If you were using MVC, there are JSON methods, for output, but it is also possible to use action filters to deserialise json that is passed in. There are some quirks to this but it does work well with json submitted via jQuery etc. 如果您使用的是MVC,则有用于输出的JSON方法,但是也可以使用操作过滤器反序列化传入的json。对此有一些奇怪之处,但它与通过jQuery等提交的json一起使用时效果很好。

Check this out 看一下这个

JavaScriptSerializer (至少内置在Framework 3.5及更高版本中)附带了花哨的新“ AJAX”内容,它不如XML序列化器那么灵活,但是在许多情况下它可以完成工作。

I believe there are some built in, but I don't think they are terribly powerful by themselves. 我相信其中有一些内置功能,但我认为它们本身并没有强大的功能。 We have always used this library. 我们一直使用此库。 It's very powerful and easy to use. 它非常强大且易于使用。

It's fantastic for serializing objects and lists or deserializing JSON. 对对象和列表进行序列化或对JSON反序列化而言,这是很棒的选择。

http://json.codeplex.com/ http://json.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM