【问题标题】:JSON serialize/deserialize with Dictionary using .NET DataContractJsonSerializer serializer使用 .NET DataContractJsonSerializer 序列化器使用 Dictionary 进行 JSON 序列化/反序列化
【发布时间】:2014-10-21 22:11:01
【问题描述】:

首先.. 我对 JSON.NET 或任何其他解析器不感兴趣。仅 DataContractJsonSerializer

我必须使用我获得并发送到 REST API 的结构,它们看起来像这样:

{           "records": [
        {
            "attributes": {
                "OBJECTID": 1,
                "Address": "380 New York St.",
                "City": "Redlands",
                "Region": "CA",
                "Postal": "92373"
            }
        },
   {
            "attributes": {
                "OBJECTID": 2,
                "Address": "1 World Way",
                "City": "Los Angeles",
                "Region": "CA",
                "Postal": "90045"
            }
        }
    ]

我们可以看到是这样的:

class SomeData
{
    public List<SomeRecord> Records { get; set; }
}

class SomeRecord
{
    public List<KeyValuePair<string, string>> Attributes { get; set; }
}

我如何为我的对象赋予属性,以便序列化程序可以生成这样的结构?或者我应该创建具有覆盖每个属性的属性的对象?

问题是 - 这个网络服务似乎是到处都是属性,我什至不确定所有可能的名称。因此,KVP 列表似乎是一个不错的选择,但它对我不起作用。

【问题讨论】:

    标签: c# json parsing datacontractjsonserializer


    【解决方案1】:

    以下应该可以工作,

        [DataContract]
        [KnownType(typeof(Record))]
        public class RecordList
        {
            public RecordList()
            {
                Records = new List<Record>();
            }
    
            [DataMember]
            public List<Record> Records { get; set; }
        }
    
        public class Record
        {
            public Record()
            {
                Attributes = new AttributeList();
            }
    
            [DataMember]
            public AttributeList Attributes { get; set; }
        }
    
    
        [Serializable]
        public class AttributeList : DynamicObject, ISerializable
        {
            private readonly Dictionary<string, object> attributes = new Dictionary<string, object>();
    
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                attributes[binder.Name] = value;
    
                return true;
            }
    
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                foreach (var kvp in attributes)
                {
                    info.AddValue(kvp.Key, kvp.Value);
                }
            }
        }
    
    
            [Test]
            public void TestSerialize()
            {
                var holder = new RecordList();
    
                dynamic record = new Record();
                record.Attributes.OBJECTID = 1;
                record.Attributes.Address = "380 New York St.";
                record.Attributes.City = "Redlands";
                record.Attributes.Address = "Region";
                record.Attributes.Region = "CA";
                record.Attributes.Postal = "92373";
    
                holder.Records.Add(record);
    
                var stream1 = new MemoryStream();
                var serializer = new DataContractJsonSerializer(typeof(RecordList));
                serializer.WriteObject(stream1, holder);
    
                stream1.Position = 0;
                StreamReader sr = new StreamReader(stream1);
                Console.Write("JSON form of holder object: ");
                Console.WriteLine(sr.ReadToEnd());
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      相关资源
      最近更新 更多