简体   繁体   English

ServiceStack.Text如何将类序列化为JSon

[英]ServiceStack.Text how to serialize class to JSon

Just downloaded ServiceStack.Text to use it in my ASP.NET. 刚刚下载了ServiceStack.Text以在我的ASP.NET中使用它。 I have class with many properties and would like to serialize five of them(string, integer, binary) to JSON. 我有许多属性的类,并希望将其中五个(字符串,整数,二进制)序列化为JSON。 Could anyone post simple example how to create JSon object from my class? 任何人都可以发布简单的例子如何从我的类创建JSon对象?

ServiceStack will deserialize all public properties of a POCO by default. 默认情况下,ServiceStack将反序列化POCO的所有公共属性。

If you only want to serialize just a few of the properties then you want to decorate your class with [DataContract], [DataMember] attributes (in the same way you would if you were using MS DataContractJsonSerializer), eg: 如果您只想序列化一些属性,那么您希望使用[DataContract],[DataMember]属性(与使用MS DataContractJsonSerializer时相同的方式)来装饰您的类,例如:

[DataContract]
public class MyClass
{
    public string WillNotSerializeString { get; set; }

    [DataMember]
    public string WillSerializeString { get; set; }

    [DataMember]
    public int WillSerializeInt { get; set; }

    [DataMember]
    public byte[] WillSerializeByteArray { get; set; }
}

Then you can use either the static utility methods on JsonSerializer to (De)serialize it, or the more terse extension methods, eg: 然后你可以使用JsonSerializer上的静态实用程序方法来(De)序列化它,或者更简洁的扩展方法,例如:

var dto = new MyClass { WillSerializeString = "some text" };
string json = dto.ToJson();
MyClass fromJson = json.FromJson<MyClass>();

Edit: 编辑:

As @Noah mentions (from comments) you can also use the [IgnoreDataMember] attribute to exclude a single property. 正如@Noah提到的(来自评论),您还可以使用[IgnoreDataMember]属性来排除单个属性。

You can use the [Serializable()] attribute on your custom class and then: 您可以在自定义类上使用[Serializable()]属性,然后:

JavaScriptSerializer serializer = new JavaScriptSerializer();

var Json = serializer.Serialize(myObject);

To ignore specific properties in the object you're serializing, simply place the [NonSerialized] attribure on them. 要忽略要序列化的对象中的特定属性,只需将[NonSerialized]属性放在它们上即可。

Update: 更新:

Given that you want to use ServiceStack to do your serialization, the ServiceStack website gives the following example: 鉴于您希望使用ServiceStack进行序列化,ServiceStack网站提供以下示例:

var customer = new Customer { Name="Joe Bloggs", Age=31 };
var json = JsonSerializer.SerializeToString(customer);
var fromJson = JsonSerializer.DeserializeFromString<Customer>(json);

Source: http://www.servicestack.net/mythz_blog/?p=344 资料来源: http//www.servicestack.net/mythz_blog/? p = 344

servicestack's test proves that by providing the [DataContract] and [DataMember] attribute allows you to determine which one is being serialized and which doesn't. servicestack的测试证明,通过提供[DataContract][DataMember]属性,您可以确定哪个序列化,哪个不序列化。

Test: https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/DataContractTests.cs 测试: https//github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/DataContractTests.cs

objects in test: https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/Support/DdnDtos.cs 测试中的对象: https//github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/Support/DdnDtos.cs

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

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