Csharp/C#教程:将XML转换为JSON到XML时保持json:Array属性分享


将XML转换为JSON到XML时保持json:Array属性

我有一块看起来像的XML

 Alan https://www.google.com Admin  

当我尝试将它序列化为json string json = JsonConvert.SerializeXmlNode(xml); 它忽略了名称空间

  { "person": { "@id": "1", "name": "Alan", "url": "https://www.google.com", "role": [ "Admin" ] } } 

当我将它反序列化回xml XmlDocument xml = JsonConvert.DeserializeXmlNode(json) ,我得到以下内容:

  Alan https://www.google.com Admin  

如何保留json:Array属性?

DeserializeXmlNode存在重载,它接受名为writeArrayAttribute布尔标志。 这就是你需要的:

 XmlDocument xml = JsonConvert.DeserializeXmlNode(json, null, true); 

生产:

  Alan https://www.google.com Admin  

这在语义上与原始xml相同。

XMl到JSon丢失了名称中包含’:’(冒号)的任何属性的所有信息。 这就是为什么’id’被序列化为@id但是’xmlns:json’在翻译中丢失了。

如果您可以访问原始XML,那么我建议您用连字符( – )替换冒号(:)。 在这种情况下,XML将是:

  Alan https://www.google.com Admin  

我已经检查过这个串行和反序列化到相同的输入和输出。

 var xmlString = @"Alanhttps://www.google.comAdmin"; var xml = new XmlDocument(); xml.LoadXml(xmlString); var json = JsonConvert.SerializeXmlNode(xml); var xmlDeserialized = JsonConvert.DeserializeXmlNode(json); xmlDeserialized.Should().NotBeNull(); xmlDeserialized.ShouldBeEquivalentTo(xml); //All good 

也许问题不在于如何序列化xml节点。 validation在序列化之前如何读取xml文件。 你能告诉我们吗?

这可能是另一种方法定制json转换器有点长,但我认为它更有用

 public class CustomXmlToJsonConverter : JsonConverter { private readonly Type[] _types; public CustomXmlToJsonConverter(params Type[] types) { _types = types; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { JToken t = JToken.FromObject(value); if (t.Type != JTokenType.Object) { t.WriteTo(writer); } else { JObject o = (JObject)t; IList propertyNames = o.Properties().Select(p => p.Name).ToList(); o.AddFirst(new JProperty("Keys", new JArray(propertyNames))); o.WriteTo(writer); } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter."); } public override bool CanRead { get { return false; } } public override bool CanConvert(Type objectType) { return _types.Any(t => t == objectType); } } 

用法

  string json = JsonConvert.SerializeObject(root,Formatting.Indented,new CustomXmlToJsonConverter(typeof(XElement))); 

结果

 { "Keys": [ "person" ], "person": { "@json": "https://james.newtonking.com/projects/json", "@id": "1", "name": "Alan", "url": "https://www.google.com", "role": { "@Array": "true", "#text": "Admin" } } } 

样本数据

上述就是C#学习教程:将XML转换为JSON到XML时保持json:Array属性分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  XNamespace jsonPrefix1 = "xmlns"; XNamespace jsonPrefix2 = "json"; XElement root = new XElement("person", new XAttribute(jsonPrefix1 + "json", "https://james.newtonking.com/projects/json"), new XAttribute("id","1"), new XElement("name", "Alan"), new XElement("url", "https://www.google.com"), new XElement("role" ,"Admin", new XAttribute(jsonPrefix2 + "Array", "true")) ); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/959020.html

(0)
上一篇 2021年11月23日
下一篇 2021年11月23日

精彩推荐