如何在C#中无需反序列化从JSON获取最小low值及对应high值?
关于无需反序列化的JSON最小值查找
首先得澄清一点:严格来说,要操作JSON中的具体字段值,必须先把JSON字符串解析(也就是反序列化)成编程语言可操作的数据结构(比如JS里的对象/数组,C#里的JObject或动态对象),不存在完全不解析就能直接提取值的方法。不过我们可以用更简洁的原生API替代自定义遍历函数,让代码更清爽。
比如在JavaScript中,你可以用Array.reduce()一步找到包含最小low值的元素,比你之前的自定义函数更简洁:
// 假设jsonStr是你的原始JSON字符串 const jsonStr = '{"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(这一步是必须的,否则无法访问内部字段) const data = JSON.parse(jsonStr); // 用reduce找到最小low对应的元素 const minLowItem = data.prices.reduce((prev, current) => { return current.low < prev.low ? current : prev; }, data.prices[0]); console.log(`最小low值: ${minLowItem.low}, 对应high值: ${minLowItem.high}`);
如果是用jQuery,也可以直接对解析后的数组用这个逻辑,不用自己写循环函数。
C#实现方案
C#里有两种常见的实现方式,分别适合不同场景:
1. 强类型反序列化(推荐,类型安全)
先定义对应JSON结构的实体类,然后反序列化后用LINQ查找:
using Newtonsoft.Json; // 需要安装Newtonsoft.Json NuGet包,或者用System.Text.Json using System.Collections.Generic; using System.Linq; // 定义实体类 public class PriceEntry { public string frequency { get; set; } public string date { get; set; } public bool intraperiod { get; set; } public double open { get; set; } public double high { get; set; } public double low { get; set; } public double close { get; set; } } public class PriceRoot { public List<PriceEntry> prices { get; set; } } // 主逻辑 string jsonString = @"{""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}]}"; // 反序列化(用Newtonsoft.Json) var priceData = JsonConvert.DeserializeObject<PriceRoot>(jsonString); // 用LINQ找到最小low的元素 var minLowEntry = priceData.prices.OrderBy(entry => entry.low).FirstOrDefault(); if (minLowEntry != null) { System.Console.WriteLine($"最小low值: {minLowEntry.low}, 对应high值: {minLowEntry.high}"); }
如果用.NET Core/.NET 5+自带的System.Text.Json,只需把反序列化代码换成:
using System.Text.Json; var priceData = JsonSerializer.Deserialize<PriceRoot>(jsonString);
2. 动态解析(无需定义实体类,适合临时场景)
如果你不想定义实体类,可以用Newtonsoft.Json的JObject进行动态解析:
using Newtonsoft.Json.Linq; using System.Linq; string jsonString = @"{""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}]}"; // 解析为JObject var jObj = JObject.Parse(jsonString); // 遍历prices数组,找到最小low的元素 var minLowEntry = jObj["prices"] .Children<JObject>() .OrderBy(item => (double)item["low"]) .FirstOrDefault(); if (minLowEntry != null) { System.Console.WriteLine($"最小low值: {minLowEntry["low"]}, 对应high值: {minLowEntry["high"]}"); }
内容的提问来源于stack exchange,提问作者Neo
相关产品推荐
相关产品推荐

