【问题标题】:convert a Json value to Integer with Newtonsoft使用 Newtonsoft 将 Json 值转换为整数
【发布时间】:2015-04-28 02:38:03
【问题描述】:

我正在使用以下代码向我的 xml 添加一个属性,以指定该节点在使用 JsonConvert.SerializeXmlNode 时应返回一个整数值。

我已将 Newtonsoft 的更新合并到我引用的 dll 中。

我正在使用以下代码添加属性:

ele.SetAttribute("Integer", "http://james.newtonking.com/projects/json", "true");

ele 来自XmlElement ele = node as XmlElement;

结果总是这样结束:

 "id": {
        "@Type": "Integer",
        "#text": "759263947"
      },

但我需要的是

"id": 759263947

请注意,我使用完全相同的语法来标识一个数组:

ele.SetAttribute("Array", "http://james.newtonking.com/projects/json", "true");

效果很好。

劳拉

【问题讨论】:

  • 我找不到任何表明 Json.NET 支持 "{http://james.newtonking.com/projects/json}Integer" = "true"documentation。据我所知,仅支持 Array。你见过别的吗?另外,您的 XML 是什么样的?
  • 建议更改此处发布的源并带有链接...stackoverflow.com/questions/18611139/…
  • 937467375966google.com/search?q=937467375966</tracking_url> 7592639471.00000
  • 您是否在此处使用源更改? stackoverflow.com/questions/18611139/…
  • 是的,我正在使用源更改(将 XmlNodeConverter.cs 替换为发布的代码)。使用这个调用,其中 NewDoc = 我在上面发布的 Xml:strJSON = JsonConvert.SerializeXmlNode(NewDoc, Newtonsoft.Json.Formatting.Indented);

标签: xml json json.net


【解决方案1】:

如果您使用this answer 中描述的XmlNodeConverter 的变体版本并在此处提供:https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs,您似乎需要这样做:

ele.SetAttribute("Type", "http://james.newtonking.com/projects/json", "Integer");

或者,对于双精度值:

ele.SetAttribute("Type", "http://james.newtonking.com/projects/json", "Float");

或者,您可以使用开箱即用的 Linq-to-JSON 手动修改将字符串值转换为数值,例如:

        string xml = @"<fulfillment xmlns:json=""http://james.newtonking.com/projects/json""><tracking_number>937467375966</tracking_number><tracking_url>google.com/search?q=937467375966</tracking_url>; <line_items json:Array=""true""><id>759263947</id><quantity>1.00000</quantity></line_items></fulfillment>";
        var doc = new XmlDocument();
        doc.LoadXml(xml);

        var obj = JObject.Parse(JsonConvert.SerializeXmlNode(doc));
        foreach (var value in obj.Descendants().OfType<JValue>().Where(v => v.Type == JTokenType.String))
        {
            long lVal;
            if (long.TryParse((string)value, out lVal))
            {
                value.Value = lVal;
                continue;
            }
            double dVal;
            if (double.TryParse((string)value, out dVal))
            {
                value.Value = dVal;
                continue;
            }
            decimal dcVal;
            if (decimal.TryParse((string)value, out dcVal))
            {
                value.Value = dcVal;
                continue;
            }
        }
        var json = obj.ToString();
        Debug.WriteLine(json);

【讨论】:

  • 一旦我正确处理了你所展示的语法(不敢相信我错过了......),我必须在序列化之前将我的值 1.0000 转换为整数,因为“ Integer" 类型在具有该值的 Newtonsoft 代码中崩溃。谢谢@dbc!我现在很摇滚。
  • 我也很兴奋 - 由于其他代码依赖于特定的代码,因此无法更改 Newtonconverter 的版本。 Linq-to-JSON 的事情,加上布尔值节省了我的一天(实际上是整个星期)
  • Linq-to-JSON 答案无法在我的机器上编译 'OfType()' 但它引导我逐步遍历所有后代并更改我想要的每个属性的值改变。
  • @KeithJohnHutchison - 这是 System.Core.dll 中的扩展方法 System.Linq.Enumerable.OfType&lt;TResult&gt;()。您可能需要添加 using System.Linq; 语句。
  • 谢谢@dbc。添加“使用 System.Linq;”整理了我的编译问题。
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 2022-01-22
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多