2017-05-09 52 views
0

我有一個類,有一個對象:Newtonsoft.Json反序列化XmlAnyAttribute?

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34283")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)] 
public partial class Scores : baseModel 
{ 
    private System.Xml.XmlAttribute[] anyAttrField; 
    /// <remarks/> 
    [System.Xml.Serialization.XmlAnyAttributeAttribute()] 
    public System.Xml.XmlAttribute[] AnyAttr 
    { 
     get 
     { 
     return this.anyAttrField; 
     } 
     set 
     { 
     this.anyAttrField = value; 
     } 
    } 
} 

從父類(它的片段):

public parial class LandingPage : baseModel 
{ 
    private string projectNameField; 
    private Scores scoresField; 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public string projectName 
    { 
     get { return this.projectNameField; } 
     set { this.projectNameField = value; } 
    } 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public Scores scores 
    { 
     get { return this.scoresField; } 
     set { this.scoresField = value } 
    } 
} 

JSON字符串我想一起工作:

{ 
    "projectName":"PROJECTTEST", 
    "scores":{ 
     "browLocker":100, 
     "heavyAd":0, 
     "walletRedirection":0 
    } 
} 

NewtonSoft.JsonConvert忽略了孩子的分數...領域

哪有我輕鬆將其轉換爲工作?

+3

爲什麼Json.NET會關心XML屬性?爲什麼不創建一對真正匹配JSON文檔的類? –

+1

爲什麼當你只能使用JSON時,試圖將JSON轉換爲XML會讓你的生活更加艱難? – Rafael

+0

將您的'Scores'類改爲'class Scores {public int BrowLocker {get; set;} public int HeavyAd {get; set;} public int WalletRedirection {get; set;}}'。你不需要任何其他的反序列化這個字符串 –

回答

0

鑑於您的Scores類型有沒有自己的特性序列化,你可以爲它創建一個custom JsonConverterXmlAttribute []陣列爲Dictionary<string, string>轉換再序列該字典來代替Scores對象:

public class XmlAttributeArrayConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(XmlAttribute[]); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     if (reader.TokenType == JsonToken.Null) 
      return null; 
     var dict = serializer.Deserialize<Dictionary<string, string>>(reader); 
     var doc = new XmlDocument(); 
     return dict.Select(p => { var a = doc.CreateAttribute(p.Key); a.Value = p.Value; return a; }).ToArray(); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     // TODO: determine how to represent XmlAttribute values with non-empty NamespaceURI - or throw an exception. 
     var attributes = (IEnumerable<XmlAttribute>)value; 
     var dict = attributes.ToDictionary(a => a.Name, a => a.Value); 
     serializer.Serialize(writer, dict); 
    } 
} 

class ScoresConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(Scores); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     if (reader.TokenType == JsonToken.Null) 
      return null; 
     var attributes = (XmlAttribute[])new XmlAttributeArrayConverter().ReadJson(reader, typeof(XmlAttribute[]), null, serializer); 
     return new Scores { AnyAttr = attributes }; 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     var scores = (Scores)value; 
     if (scores.AnyAttr == null) 
      writer.WriteNull(); 
     else 
     { 
      new XmlAttributeArrayConverter().WriteJson(writer, scores.AnyAttr, serializer); 
     } 
    } 
} 

(注意我提取一些邏輯到一個單獨的XmlAttributeArrayConverter在情況下,它可能是使用的其他地方)

如下它然後應用於Scores

[JsonConverter(typeof(ScoresConverter))] 
public partial class Scores 
{ 
} 

樣品fiddle

請注意,如果其中一個XML屬性碰巧位於名稱空間中(大部分時間爲none will be),則命名空間不會被序列化。如果您的屬性可能具有名稱空間,則需要決定如何將屬性的全名轉換爲JSON屬性名稱。 {namespace}localname將是一種可能性,那就是XName.ToString()所使用的。

相關問題