.NET JSON序列化:如何使一个DataContract成为另一个DataContract的成员 [英] .NET JSON Serialization: How to have a DataContract as a member of another DataContract

查看:107
本文介绍了.NET JSON序列化:如何使一个DataContract成为另一个DataContract的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化一个.net对象,该对象包含另一个数据协定对象作为参数.代码如下...

I am trying to serialize a .net object contains another data contract object as a parameter. The code is as follows...

[DataContract]
        public class JsonObject2
        {
            [DataMember(Name = "field1")]
            string field1 { get; set; }
            [DataMember(Name = "field2")]
            string field2 { get; set; }
            [DataMember(Name = "field3")]
            object[][] test = { new object[]{"key1", "value1"}, new object[]{"key2", "value2"}, new object[]{"key3", "value3"} };
        }

        [DataContract]
        public class JsonObject3
        {
            [DataMember(Name = "field1")]
            public string field1 { get; set; }
            [DataMember(Name = "field2")]
            public object field2 { get; set; }
        }

DataContractJsonSerializer dcjs2 = new DataContractJsonSerializer(typeof(JsonObject3));

                JsonObject3 obj3 = new JsonObject3();
                obj3.field1 = "hello";
                obj3.field2 = new JsonObject2();
                dcjs2.WriteObject(s, obj3);
                s.Position = 0;
                MessageBox.Show(new StreamReader(s).ReadToEnd());

执行上述操作会导致以下异常...

Doing the above, results in the following exception...

"{"不应使用数据协定名称为'Form1.JsonObject2:http://schemas.datacontract.org/2004/07/JSONParser'的类型'JSONParser.Form1 + JsonObject2'.将任何静态未知的类型添加到已知类型的列表中,例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型的列表中.}"

"{"Type 'JSONParser.Form1+JsonObject2' with data contract name 'Form1.JsonObject2:http://schemas.datacontract.org/2004/07/JSONParser' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."}"

我不知道如何执行该例外中给出的任何建议.

I can't figure out how to carry out any of the recommendations given in that exception.

  1. 如何添加到KnownTypes列表中?
  2. 如何使用KnownTypeAttribute?

推荐答案

好,找出了解决方法.如果有人发现它有用,我不想删除这个问题,所以这里是解决方法...

Ok, figured out how to do it. I didn't want to delete this question in case someone else finds it useful, so here is the solution...

[DataContract]
    [KnownType(typeof(JsonObject2))]
    public class JsonObject2
    {
        [DataMember(Name = "field1")]
        string field1 { get; set; }
        [DataMember(Name = "field2")]
        string field2 { get; set; }
        [DataMember(Name = "field3")]
        object[][] test = { new object[]{"key1", "value1"}, new object[]{"key2", "value2"}, new object[]{"key3", "value3"} };
    }

    [DataContract]
    public class JsonObject3
    {
        [DataMember(Name = "field1")]
        public string field1 { get; set; }    
        [DataMember(Name = "field2")]
        public object field2 { get; set; }
    }

注意JsonObject2类的声明上方的[KnownType(typeof(JsonObject2))]吗?解决了.​​

Notice the [KnownType(typeof(JsonObject2))] above the declaration of the JsonObject2 class ? That solved it.

这篇关于.NET JSON序列化:如何使一个DataContract成为另一个DataContract的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆