有什么简单的方法可以从json中找出最小值而不进行反序列化C#

我从api下面有json输出。 我想找出所有元素中的最低值。

json数据-

{
  "prices": [   
    {
      "frequency": "daily",
      "date": "2020-05-05",
      "intraperiod": false,
      "open": 295.06,
      "high": 301.0,
      "low": 294.46,
      "close": 297.56
    },
    {
      "frequency": "daily",
      "date": "2020-05-04",
      "intraperiod": false,
      "open": 289.17,
      "high": 293.69,
      "low": 112.1,
      "close": 293.16
    },
    {
      "frequency": "daily",
      "date": "2020-05-01",
      "intraperiod": false,
      "open": 286.25,
      "high": 299.0,
      "low": 222,
      "close": 289.07
    }
  ]
}

I want to compare all values among json element and display lowest value = "low": 112.1 and its own high value. "high": 293.69,

我尝试像下面使用jQuery,但我想在C#中做到这一点,请分享C#代码-

function get(arr, prop) {
    var min;
    for (var i=0 ; i<arr.length ; i++) {
        if (min== null || parseInt(arr[i][prop]) > parseInt(min[prop]))
            min= arr[i];
    }
    return min;
}

var min = get(arr, "low");
console.log(min.high);
评论
XOC莫
XOC莫

You may use Json.Linq for that, parse your JSON to JObject, then get all properties of it and find the lowest and highest values

var json = JObject.Parse(jsonString);

var properties = json.DescendantsAndSelf().OfType<JProperty>();
var low = properties.Where(p => p.Name == "low").Min(p => p.Value.Value<double>());
var high = properties.Where(p => p.Name == "high").Min(p => p.Value.Value<double>());
点赞
评论