有没有什么简单的方法可以在不进行反序列化的情况下从 json 中找出最低值 c#
is there any simple way to find out lowest value from json without doing de-serialization 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
}
]
}
我想比较 json 元素中的所有值并显示最低值 = "low": 112.1
和它自己的最高值。 "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);
您可以为此使用 Newtonsoft.Json.Linq
,将 JSON 解析为 JObject
,然后获取名称为 low
的所有属性,找到 属性最低值并获得同级别的high
值
var json = JObject.Parse(jsonString);
var properties = json.DescendantsAndSelf()
.OfType<JProperty>()
.Where(p => p.Name == "low");
var lowProperty = properties
.Aggregate((p1, p2) => p1.Value.Value<double>() < p2.Value.Value<double>() ? p1 : p2);
var highProperty = (lowProperty?.Parent as JObject)?.Property("high");
Console.WriteLine(lowProperty);
Console.WriteLine(highProperty);
它给你
"low": 112.1
"high": 293.69
您可以使用正则表达式。
var pattern = "\"high\": ([0-9]+(\.[0-9]+)?)";
MatchCollection matches = Regex.Matches(json, pattern);
var max = double.MinValue;
foreach (Match m in matches) {
var val = Convert.ToDouble(m.Groups[1].Value);
if (max < val) {
max = val;
}
}
同样适用于低价值。
将其反序列化是可行的方法
您可以将其反序列化为一个对象并进行您想要的计算
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var priceData = PriceData.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class PriceData
{
[JsonProperty("prices")]
public List<Price> Prices { get; set; }
}
public partial class Price
{
[JsonProperty("frequency")]
public string Frequency { get; set; }
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("intraperiod")]
public bool Intraperiod { get; set; }
[JsonProperty("open")]
public double Open { get; set; }
[JsonProperty("high")]
public double High { get; set; }
[JsonProperty("low")]
public double Low { get; set; }
[JsonProperty("close")]
public double Close { get; set; }
}
public partial class PriceData
{
public static PriceData FromJson(string json) => JsonConvert.DeserializeObject<PriceData>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this PriceData self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
你可以使用 Quciktype 从 json 中获取 POCO class (上面的代码是从中生成的)
然后使用
var data = PriceData.FromJson(json_string);
var min = data.Select(x=>x.Low).Min();
// There may be multiple Price objects with the same low, I will use the first one
min_object = data.Where(x=>x.Low == min).First()
var high_value_of_min_object = min_object.High;
您可能想决定要查找的元素
P.S我没有测试代码。
您可以使用 quicktype.io:
等工具创建 类 来代表您的 JSON
public partial class Temperatures
{
[JsonProperty("prices")]
public List<Price> Prices { get; set; }
}
public partial class Price
{
[JsonProperty("frequency")]
public string Frequency { get; set; }
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("intraperiod")]
public bool Intraperiod { get; set; }
[JsonProperty("open")]
public double Open { get; set; }
[JsonProperty("high")]
public double High { get; set; }
[JsonProperty("low")]
public double Low { get; set; }
[JsonProperty("close")]
public double Close { get; set; }
}
然后使用 Json.NET, and use MinBy
from MoreLinq 反序列化您的 json 以获得 Low
的最低价格。 MinBy
将 return 变成 IEnumerable<Price>
,因此我们可以迭代每个最低价格并打印 Low
和 High
属性:
using Newtonsoft.Json;
using MoreLinq;
...
var deserializedJson = JsonConvert.DeserializeObject<Temperatures>(json);
var minPrices = deserializedJson.Prices.MinBy(price => price.Low);
foreach (var price in minPrices)
{
Console.WriteLine($"Min = {price.Low}, High = {price.High}");
}
输出:
Min = 112.1, High = 293.69
上的完整演示
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
}
]
}
我想比较 json 元素中的所有值并显示最低值 = "low": 112.1
和它自己的最高值。 "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);
您可以为此使用 Newtonsoft.Json.Linq
,将 JSON 解析为 JObject
,然后获取名称为 low
的所有属性,找到 属性最低值并获得同级别的high
值
var json = JObject.Parse(jsonString);
var properties = json.DescendantsAndSelf()
.OfType<JProperty>()
.Where(p => p.Name == "low");
var lowProperty = properties
.Aggregate((p1, p2) => p1.Value.Value<double>() < p2.Value.Value<double>() ? p1 : p2);
var highProperty = (lowProperty?.Parent as JObject)?.Property("high");
Console.WriteLine(lowProperty);
Console.WriteLine(highProperty);
它给你
"low": 112.1
"high": 293.69
您可以使用正则表达式。
var pattern = "\"high\": ([0-9]+(\.[0-9]+)?)";
MatchCollection matches = Regex.Matches(json, pattern);
var max = double.MinValue;
foreach (Match m in matches) {
var val = Convert.ToDouble(m.Groups[1].Value);
if (max < val) {
max = val;
}
}
同样适用于低价值。
将其反序列化是可行的方法
您可以将其反序列化为一个对象并进行您想要的计算
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var priceData = PriceData.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class PriceData
{
[JsonProperty("prices")]
public List<Price> Prices { get; set; }
}
public partial class Price
{
[JsonProperty("frequency")]
public string Frequency { get; set; }
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("intraperiod")]
public bool Intraperiod { get; set; }
[JsonProperty("open")]
public double Open { get; set; }
[JsonProperty("high")]
public double High { get; set; }
[JsonProperty("low")]
public double Low { get; set; }
[JsonProperty("close")]
public double Close { get; set; }
}
public partial class PriceData
{
public static PriceData FromJson(string json) => JsonConvert.DeserializeObject<PriceData>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this PriceData self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
你可以使用 Quciktype 从 json 中获取 POCO class (上面的代码是从中生成的)
然后使用
var data = PriceData.FromJson(json_string);
var min = data.Select(x=>x.Low).Min();
// There may be multiple Price objects with the same low, I will use the first one
min_object = data.Where(x=>x.Low == min).First()
var high_value_of_min_object = min_object.High;
您可能想决定要查找的元素 P.S我没有测试代码。
您可以使用 quicktype.io:
等工具创建 类 来代表您的 JSONpublic partial class Temperatures
{
[JsonProperty("prices")]
public List<Price> Prices { get; set; }
}
public partial class Price
{
[JsonProperty("frequency")]
public string Frequency { get; set; }
[JsonProperty("date")]
public DateTimeOffset Date { get; set; }
[JsonProperty("intraperiod")]
public bool Intraperiod { get; set; }
[JsonProperty("open")]
public double Open { get; set; }
[JsonProperty("high")]
public double High { get; set; }
[JsonProperty("low")]
public double Low { get; set; }
[JsonProperty("close")]
public double Close { get; set; }
}
然后使用 Json.NET, and use MinBy
from MoreLinq 反序列化您的 json 以获得 Low
的最低价格。 MinBy
将 return 变成 IEnumerable<Price>
,因此我们可以迭代每个最低价格并打印 Low
和 High
属性:
using Newtonsoft.Json;
using MoreLinq;
...
var deserializedJson = JsonConvert.DeserializeObject<Temperatures>(json);
var minPrices = deserializedJson.Prices.MinBy(price => price.Low);
foreach (var price in minPrices)
{
Console.WriteLine($"Min = {price.Low}, High = {price.High}");
}
输出:
Min = 112.1, High = 293.69
上的完整演示