简体   繁体   English

C#:在 object 中是否可以将十进制值转换为字符串或双精度值?

[英]C#: In an object is it possible to convert decimal values to a string or double?

I have a Json object which I get from rest call and there is no specific format.我有一个 Json object ,我是从 rest 调用中获得的,并且没有特定的格式。 Is it possible to iterate through the object and check if any decimal value exists and convert it to a string/double?是否可以遍历 object 并检查是否存在任何十进制值并将其转换为字符串/双精度值?

I need to pass this object into a couchbaseLite's MutableDocument which doesn't allow any decimals我需要将此 object 传递到不允许任何小数的 couchbaseLite 的 MutableDocument

Object Object

{
 "performer": [
    {
      "referedBy": "ABDC",
      "text": "XYZ"
    }
  ],
  "quantity": {
    "value": 100.0, **// --> This should bee converted to "100.0"**
    "system": "http://unitsofmeasure.org",
  },
"range": [
    {
      "low": {
        "value": 0.0, **// --> This should bee converted to "0.0"**
        "system": "http://unitsofmeasure.org",
      },
      "text": "Total values"
    }
  ]
}

I tried to do it by checking the specific properties and fixing it ie, check for "quantity", "range" etc. But wondering if there is a better way to iterate through the object ie, a generic method that can iterate through the object that can convert the decimals to integer/double as I won't know the exact format of the object beforehand.我试图通过检查特定属性并修复它来做到这一点,即检查“数量”、“范围”等。但想知道是否有更好的方法来迭代 object,即一种可以迭代 object 的通用方法可以将小数转换为整数/双精度,因为我事先不知道 object 的确切格式。

Code :代码

private static void UpdateQuantity(JToken result)
        {
            var quantity = (result as JToken)["quantity"];
            if (quantity == null)
            {
                return;
            }
            //update value from deciaml to string in quantity
            string quantityValue = null;
            foreach (JToken token in (result as JToken)["quantity"].Children())
            {
                JProperty ps = token as JProperty;
                if (ps != null && ps.Name.Contains("value"))
                {
                    quantityValue = ps.Value.ToString().Replace(',', '.');
                    ps.Replace(new JProperty("value", quantityValue as object));
                    break;
                }
            }
        }

The following code should work for you以下代码应该适合您

foreach (var token in
    obj.DescendantsAndSelf().OfType<JObject>().SelectMany(o =>
        o.Properties().Where(p => p.Value.Type == JTokenType.Integer || p.Value.Type == JTokenType.Float)))
{
    token.Value = token.Value.ToString();
}

dotnetfiddle dotnetfiddle

  • Take all descendants of the root object, including itself.取根 object 的所有后代,包括它自己。
  • Which are JObject哪些是JObject
  • Select all their properties where... Select 的所有属性...
  • ... the JSON type of the property value is either Integer or Float ...属性值的 JSON 类型是IntegerFloat
  • For each one, change the value to be the stringified form of the same value.对于每一个,将值更改为相同值的字符串化形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM