How to convert JSON to XML or XML to JSON?

后端 未结 13 1296
借酒劲吻你
借酒劲吻你 2020-11-22 05:43

I started to use Json.NET to convert a string in JSON format to object or viceversa. I am not sure in the Json.NET framework, is it possible to convert a string in JSON to X

13条回答
  •  孤街浪徒
    2020-11-22 06:13

    Yes. Using the JsonConvert class which contains helper methods for this precise purpose:

    // To convert an XML node contained in string xml into a JSON string   
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc);
    
    // To convert JSON text contained in string json into an XML node
    XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
    

    Documentation here: Converting between JSON and XML with Json.NET

提交回复
热议问题