Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

xml

json.net

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 be the best approach for this?

I know I can do a straight up replace, but an @ character may be relevant somewhere and shouldn't be replaced. Is there a Regex for this?

public ActionResult Layout()
{
    var xml = new XmlDocument();
    xml.XmlResolver = null;
    xml.Load(Server.MapPath("~/App_Data/Navigation.xml"));
    return Content(JsonConvert.SerializeXmlNode(xml, Newtonsoft.Json.Formatting.Indented));
}
{
  "Layout": {
    "Navigation": [
      {
        "@Type": "Menu",
        "@Title": "Dashboard"
      },
      {
        "@Type": "Menu",
        "@Route": "Events",
        "@Title": "Events",
        "Navigation": {
          "@Type": "Action",
          "@Route": "Event",
          "@Title": "+ Add Event",
          "@Order": "1",
          "Navigation": {
            "@Type": "Item",
            "@Route": "Event",
            "@Name": "Event",
            "Navigation": [
              {
                "@Route": "Pools",
                "@Type": "SubNavigation",
                "@Name": "Pools"
              },
              {
                "@Route": "Brackets",
                "@Type": "SubNavigation",
                "@Name": "Brackets"
              }
            ]
          }
        }
      }
    ]
  }
}
like image 839
Mike Flynn Avatar asked Sep 02 '11 02:09

Mike Flynn


People also ask

How do I change XML format to JSON?

To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.

Can JSON be converted to XML?

A JSON is a lightweight data-interchange format and the format of JSON is like a key-value pair. We can convert a JSONObject into an XML format using org. json. XML class, this provides static methods to convert an XML text into a JSONObject and to convert a JSONObject into an XML text.

Can we convert XML to JSON in C#?

Once you have an XmlDocument object, you can use Json.NET to convert that object into a Json representation. var json = JsonConvert. SerializeXmlNode(doc, Formatting.

Why would you convert XML to JSON?

The purpose of the XML to JSON Converter tool is to convert XML (Extensible Markup Language) code into JSON format. JSON is a lightweight data-interchange format, it's useful for exchanging data from client to server, or from server to client.


2 Answers

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"}}}
like image 132
Ilya Avatar answered Sep 27 '22 19:09

Ilya


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));
}
like image 27
Mike Flynn Avatar answered Sep 27 '22 20:09

Mike Flynn