【问题标题】:Cannot deserialize xml string with Newtonsoft.Json.JsonConvert.DeserializeObject无法使用 Newtonsoft.Json.JsonConvert.DeserializeObject 反序列化 xml 字符串
【发布时间】:2013-11-15 15:58:58
【问题描述】:

您好,我将 xml 作为字符串传递

<AbcDto xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Abc">
  <Id>2</Id>
  <Description>sample string 4</Description>
  <Name>sample string 3</Name>
  <PolicyId>c17f5b9f-c9bf-4a3a-b09b-f44ec84b0d00</PolicyId>
  <Status>Active</Status>
  <TimeZoneId>USCentral</TimeZoneId>
</AbcDto>

当我尝试为 Web Api 创建自定义模型绑定器时

   public bool BindModel(System.Web.Http.Controllers.HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var json = actionContext.Request.Content.ReadAsStringAsync().Result;
            if (!string.IsNullOrEmpty(json))
            {
                var jsonObject = (JObject) Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                var jsonPropertyNames = jsonObject.Properties().Select(p => p.Name).ToList();

作为参数传递给下面方法的json字符串是一个xml字符串 我在 Newtonsoft.Json.JsonConvert.DeserializeObject(json); 面临异常; 异常详情: 解析值时遇到意外字符:

【问题讨论】:

    标签: json asp.net-mvc-4 c#-3.0 json.net json-deserialization


    【解决方案1】:

    您收到一个错误,因为 JsonConvert.DeserializeObject() 需要 JSON 输入,而不是 XML。如果您想使用JObject 处理 XML,您需要先将 XML 转换为 JSON。 JsonConvert 类有一个用于此目的的 SerializeXmlNode() 方法。

    演示:

    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
            <AbcDto xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Abc"">
              <Id>2</Id>
              <Description>sample string 4</Description>
              <Name>sample string 3</Name>
              <PolicyId>c17f5b9f-c9bf-4a3a-b09b-f44ec84b0d00</PolicyId>
              <Status>Active</Status>
              <TimeZoneId>USCentral</TimeZoneId>
            </AbcDto>";
    
            // If the json string contains XML, convert it to JSON
            if (json.TrimStart().StartsWith("<"))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(json);
                json = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);
            }
    
            // Now you can load the JSON into a JObject
            var jsonObject = JObject.Parse(json);
            var jsonPropertyNames = jsonObject.Properties().Select(p => p.Name).ToList();
            foreach (string name in jsonPropertyNames)
            {
                Console.WriteLine(name);
            }
        }
    }
    

    输出:

    @xmlns:i
    @xmlns
    Id
    Description
    Name
    PolicyId
    Status
    TimeZoneId
    

    【讨论】:

    • 感谢回复。
    • 但我面临的问题是我可以发送 Json 对象或 XML。单一方法需要处理这两种情况。
    • 啊。好吧,你从来没有在你的问题中提到过。幸运的是,这很容易解决;只需添加一个检查以检测字符串是否为 XML。如果是,则将其转换为 JSON。如果没有,请照常解析。我已经更新了我的答案来演示。
    • 对不起。我会尝试解决方案。感谢您的回复。
    猜你喜欢
    • 2021-06-07
    • 2018-10-22
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多