简体   繁体   English

使用Json.NET进行序列化

[英]Serialization with Json.NET

I'm building a Silverlight wp7 app in C#. 我正在用C#构建Silverlight wp7应用程序。 I have objects that I want to convert to and from JSON. 我有想要与JSON相互转换的对象。 I'm using JSON.NET. 我正在使用JSON.NET。

Several properties of these objects require a bit of logic to initialize. 这些对象的几个属性需要一些逻辑来初始化。 Is there some way to use a custom converter method? 有什么方法可以使用自定义转换器方法? (One such property is a List of strings. The data is given as a single String, and in the constructor the class splits it into a list.) (一个这样的属性是字符串列表。数据作为单个字符串提供,并且在构造函数中,类将其拆分为一个列表。)

Also, I'd rather have the properties be read only, but they have to be read-write for conversation (right?). 另外,我宁愿属性是只读的,但对于对话它们必须是可读写的(对吗?)。 That's kind of a pain. 有点痛苦。

Or am I stuck doing the conversion manually? 还是我坚持手动进行转换?

Writing a JsonConverter allows you to manually serialize/deserialize a type. 编写JsonConverter允许您手动序列化/反序列化类型。 You could write one for a List that will split the string when reading and concatenate it when writing JSON. 您可以为List编写一个,在读取时拆分字符串,在编写JSON时将其串联。

If you are calling for the objects through a web service, you can change the encoding of the web service response to return JSON. 如果要通过Web服务调用对象,则可以更改Web服务响应的编码以返回JSON。

http://blog.davebouwman.com/posting-data-to-aspnet-json-services-with-doj http://blog.davebouwman.com/posting-data-to-aspnet-json-services-with-doj

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

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