How to access attributes of a JSON that was converted with special characters?

1

I'm consuming the Microsoft translation API, they That is, MICROSOFT returns XML in its API's), I was able to execute everything right, but as I hate working with XML I decided to convert from XML to JSON, with the library Json.NET , the XML translation response is:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Mechanical</string>

When I convert to JSON through Json.NET:

XmlDocument doc = new XmlDocument();
doc.LoadXml(response.Content);
string json = JsonConvert.SerializeXmlNode(doc);
Debug.WriteLine(json);

I get this:

{"string":{"@xmlns":"http://schemas.microsoft.com/2003/10/Serialization/","#text":"mechanical"}}

If I try to access XML I get an error in the browser:

  

SyntaxError: illegal character

How to access string.#text if the format converter is adding special characters in the attribute name? I noticed that this occurs in the Json.NET example

    
asked by anonymous 15.03.2018 / 16:08

1 answer

1

When there are special characters in the attribute name, just call it with brackets ([]).

{"string":{"@xmlns":"http://schemas.microsoft.com/2003/10/Serialization/","#text":"mechanical"}}

Access #text like this:

console.log(string["#text"]); //mechanical
    
15.03.2018 / 18:06