Ho seguito xml;
Codice C # per l’analisi;
WebClient client = new WebClient(); StringBuilder builder = new StringBuilder(); string downloadString = client.DownloadString(XslMapperFileAddress); XmlDocument xml = new XmlDocument(); xml.LoadXml(downloadString); XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings() { OmitXmlDeclaration = true }); xml.Save(writer); string xmlString = builder.ToString(); xml.LoadXml(xmlString); string jsonText = JsonConvert.SerializeXmlNode(xml, Formatting.Indented, true); jsonText = Regex.Replace(jsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase); XslMapper xslMapper = JsonConvert.DeserializeObject(jsonText); return xslMapper.XmlMapperTypes;
Quando serializzo questo xml in json con json.net ottengo i seguenti risultati;
{ "type": [ { "name": "article", "xsl": "http://localhost:8080/Services/Xsl-a.xslt", "category": [ { "name": "1234", "xsl": "http://localhost:8080/Services/Xsl-b.xslt" }, { "name": "1234", "xsl": "http://localhost:8080/Services/Xsl-b.xslt" } ] }, { "name": "slideshow", "xsl": "http://localhost:8080/Services/Xsl-c.xslt", "category": { "name": "1234", "xsl": "http://localhost:8080/Services/Xsl-b.xslt" } } ] }
come puoi vedere la prima sezione della categoria viene analizzata come una matrice (che intendo fare) e la seconda parte convertita come object. Ecco perché sto ricevendo errore da JSON.NET
Come posso analizzare la seconda parte come array come;
"category": [ { "name": "1234", "xsl": "http://localhost:8080/Services/Xsl-b.xslt" } ] },
La conversione tra JSON e XML contiene un esempio chiamato Attribute to Force a JSON Array che dice che devi definire uno spazio dei nomi JSON
xmlns:json='http://james.newtonking.com/projects/json'
nell’elemento radice XML e aggiungere un attributo
json:Array='true'
all’elemento che desideri convertire in array (
nel tuo caso).