{ "?xml" : { "@version" : "1.0","@enCoding" : "UTF-8" },"datafeed" : { "@Feedname" : "issuerDetails","SecurityDetails" : { "Security" : { "SecurityID" : { "@IDValue" : "AAPL-NSDQ","@fiscalYearEnd" : "2016-12-31T00:00:00.00" },"FinancialModels" : { "FinancialModel" : [{ "@ID" : "780","@name" : "Estimates - Energy","@clIEntCode" : "A","Values" : [{ "@name" : "EBITDA","@clIEntCode" : "EBITDA","@currency" : "C$","Value" : [{ "@year" : "2014","#text" : "555.64" },{ "@year" : "2015","#text" : "-538.986" },{ "@year" : "2016","#text" : "554.447" },{ "@year" : "2017","#text" : "551.091" },{ "@year" : "2018","#text" : "0" } ] },{ "@name" : "EPS","@clIEntCode" : "EPS","#text" : "0" },"#text" : "-1.667" },"#text" : "-1.212" },"#text" : "0.202" },{ "@name" : "CFPS","@clIEntCode" : "CFPS","#text" : "3.196" },"#text" : "-0.207" },"#text" : "0.599" },"#text" : "2.408" },"#text" : "0" } ] } ] } ] } } } }}
如何为2015年,2016年,2017年的EPS选择#text数据?这是我到目前为止的查询:
JObject JsonFeed = JObject.Parse(JsonText);var query = from security in JsonFeed.SelectTokens("datafeed.SecurityDetails.Security") .SelectMany(i => i.ObjectsOrSelf()) let finModels = security.SelectTokens("FinancialModels.FinancialModel") .SelectMany(s => s.ObjectsOrSelf()).FirstOrDefault() where finModels != null select new { FinModelClIEntCode = (string)finModels.SelectToken("Values[1].@clIEntCode"),FinModelYear2015 = (string)finModels.SelectToken("Values[1].Value[1].@year"),FinModelValue2015 = (string)finModels.SelectToken("Values[1].Value[1].#text"),FinModelYear2016 = (string)finModels.SelectToken("Values[1].Value[2].@year"),FinModelValue2016 = (string)finModels.SelectToken("Values[1].Value[2].#text"),FinModelYear2017 = (string)finModels.SelectToken("Values[1].Value[3].@year"),FinModelValue2017 = (string)finModels.SelectToken("Values[1].Value[3].#text"),};
这是我正在使用的JsonExtensions:
public static class JsonExtensions{ public static IEnumerable<JToken> DescendantsAndSelf(this JToken node) { if (node == null) return Enumerable.Empty<JToken>(); var container = node as JContainer; if (container != null) return container.DescendantsAndSelf(); else return new[] { node }; } public static IEnumerable<JObject> ObjectsOrSelf(this JToken root) { if (root is JObject) yIEld return (JObject)root; else if (root is JContainer) foreach (var item in ((JContainer)root).Children()) foreach (var child in item.ObjectsOrSelf()) yIEld return child; else yIEld break; } public static IEnumerable<JToken> SingleOrMultiple(this JToken source) { IEnumerable<JToken> arr = source as JArray; return arr ?? new[] { source }; }}
问题是EPS对于下一家公司并不总是处于同一位置?所以,我希望查询搜索EPS客户端代码&返回上述年份的值,希望使用DRY方法.你能帮我完成我的查询吗?
注意:我实际上是在下载XML字符串,将其转换为JsON然后解析它.
Xmldocument doc = new Xmldocument();doc.LoadXml(xmlString);JsonText = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc); JObject JsonFeed = JObject.Parse(JsonText);解决方法 我认为最简单的方法是将Json反序列化为如下所示的具体对象
var root = JsonConvert.DeserializeObject<RootObject>(Jsonstring);
你的模型会是
public class SecurityID{ [JsonProperty("@IDValue")] public string IDValue { get; set; } [JsonProperty("@iscalYearEnd")] public string FiscalYearEnd { get; set; }}public class Time{ [JsonProperty("@year")] public string Year { get; set; } [JsonProperty("#text")] public string Text { get; set; }}public class FinancialModelitem{ [JsonProperty("@name")] public string name { get; set; } [JsonProperty("@clIEntCode")] public string ClIEntCode { get; set; } [JsonProperty("@currency")] public string Currency { get; set; } public List<Time> Value { get; set; }}public class FinancialModel{ [JsonProperty("@ID")] public string ID { get; set; } [JsonProperty("@name")] public string name { get; set; } [JsonProperty("@clIEntCode")] public string ClIEntCode { get; set; } public List<FinancialModelitem> Values { get; set; }}public class FinancialModels{ public List<FinancialModel> FinancialModel { get; set; }}public class Security{ public SecurityID SecurityID { get; set; } public FinancialModels FinancialModels { get; set; }}public class SecurityDetails{ public Security Security { get; set; }}public class datafeed{ [JsonProperty("@Feedname")] public string Feedname { get; set; } public SecurityDetails SecurityDetails { get; set; }}public class Xml{ [JsonProperty("@version")] public string Version { get; set; } [JsonProperty("@enCoding")] public string EnCoding { get; set; }}public class RootObject{ [JsonProperty("?xml")] public Xml Xml { get; set; } public datafeed datafeed { get; set; }}
现在你的查询就是
var result = root.datafeed.SecurityDetails.Security.FinancialModels.FinancialModel .FirstOrDefault()?.Values .FirstOrDefault(x => x.name == "EPS") .Value .Where(x => new[] { "2015","2016","2017" }.Contains(x.Year)) .Select(x => x.Text) .ToList();总结
以上是内存溢出为你收集整理的c# – LINQ to JSON – 动态嵌套数组的设置列表全部内容,希望文章能够帮你解决c# – LINQ to JSON – 动态嵌套数组的设置列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)