JSON.NET and Replacing @ Sign in XML to JSON converstion

后端 未结 10 1990
野的像风
野的像风 2020-12-11 01:32

The JSON.NET framework can convert XML to JSON, but it uses the @ sign in the JSON as the attribute. I would rather remove this before sending it to the view. What would b

相关标签:
10条回答
  • 2020-12-11 01:38

    This is my regex contribution, using lookaheads and lookbehinds to ensure it only occurs in the attribute field

    (?m)(?<=^\s+\")(@)(?=.+\"\:)

    Breakdown:

    (?m) - Run in multiline mode

    (?<=^\s+\") - Lookbehind to find the beginning of the line, one or more spaces, and a quote symbol

    (@) - Match the @ after the quote

    (?=.+\"\:) - Look ahead to match any character at least once, followed by another quote and then a colon

    0 讨论(0)
  • 2020-12-11 01:42

    I went ahead and used this. Let me know if there is a better way.

    public ActionResult Layout()
    {
        var xml = new XmlDocument();
        xml.XmlResolver = null;
        xml.Load(Server.MapPath("~/App_Data/Navigation.xml"));
    
        var jsonText = JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.Indented);
        return Content(Regex.Replace(jsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase));
    }
    
    0 讨论(0)
  • 2020-12-11 01:42

    The Regex not always work great,when content has the @ character and in the first place,that will be replace too. so I think(?<!:)(\"@)(?!.\":\\s ) may be better.

    0 讨论(0)
  • 2020-12-11 01:43

    First replace all "@" in your xml content with some placeholder (as example {{at_the_rate}}). Then use below code

    JsonConvert.SerializeXmlNode(doc).Replace("@", "").Replace("{{at_the_rate}}", "@")
    
    0 讨论(0)
  • 2020-12-11 01:47

    I suggest using the following regex, it the same as @yieio provided, but enhance to leave the content as is without any modifications, and affect only property names

    var jString = Regex.Replace(
    JsonConvert.SerializeXmlNode(content, Newtonsoft.Json.Formatting.None, true),
     "(?<=(\\,\\\"|\\{\\\"))(@)(?!.*\\\":\\\\s )", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
    0 讨论(0)
  • 2020-12-11 01:49

    It took me quite a while to find the right answer, so I thought I'd share:

    var xDocument = XDocument.Parse("<xml><a attr=\"b\">c</a></xml>");
    var builder = new StringBuilder();
    JsonSerializer.Create().Serialize(new CustomJsonWriter(new StringWriter(builder)), xDocument);
    var serialized = builder.ToString();
    
    public class CustomJsonWriter : JsonTextWriter
    {
        public CustomJsonWriter(TextWriter writer): base(writer){}
    
        public override void WritePropertyName(string name)
        {
            if (name.StartsWith("@") || name.StartsWith("#"))
            {
                base.WritePropertyName(name.Substring(1));
            }
            else
            {
                base.WritePropertyName(name);
            }
        }
    }
    

    Output:

    {"xml":{"a":{"attr":"b","text":"c"}}}
    
    0 讨论(0)
提交回复
热议问题