Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找最后一个实际报告的季度_C#_Json.net_Linq To Json - Fatal编程技术网

C# 查找最后一个实际报告的季度

C# 查找最后一个实际报告的季度,c#,json.net,linq-to-json,C#,Json.net,Linq To Json,我试图找到上一个实际报告的季度及其附带的显示值。标准是@clientsdesdescription或@displayName必须是EPS。以下是我的JSON字符串的外观: { "?xml": { "@version": "1.0", "@encoding": "UTF-8" }, "DataFeed": { "@FeedName": "issuerDetails", "SecurityDetails": {

我试图找到上一个实际报告的季度及其附带的显示值。标准是@clientsdesdescription或@displayName必须是EPS。以下是我的JSON字符串的外观:

{
    "?xml": {
        "@version": "1.0",
        "@encoding": "UTF-8"
    },
    "DataFeed": {
        "@FeedName": "issuerDetails",
        "SecurityDetails": {
            "Security": {
                "@sequence": "850",
                "TimesSeriesList": [{
                        "@description": "EPS",
                        "@clientsDescription": "EPS",
                        "FinancialValue": [{
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q1",
                                "@periodEnd": "2015-9-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.19"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q2",
                                "@periodEnd": "2015-12-31T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.26"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q3",
                                "@periodEnd": "2015-3-31T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.34"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Estimate",
                                "@period": "Q4",
                                "@periodEnd": "2015-6-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.32"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Estimate",
                                "@period": "Annual",
                                "@periodEnd": "2015-6-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$1.11"
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
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)
    {
        if (source == null)
            return Enumerable.Empty<JToken>();
        IEnumerable<JToken> arr = source as JArray;
        return arr ?? new[] { source };
    }
}   
以下是json类的外观:

{
    "?xml": {
        "@version": "1.0",
        "@encoding": "UTF-8"
    },
    "DataFeed": {
        "@FeedName": "issuerDetails",
        "SecurityDetails": {
            "Security": {
                "@sequence": "850",
                "TimesSeriesList": [{
                        "@description": "EPS",
                        "@clientsDescription": "EPS",
                        "FinancialValue": [{
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q1",
                                "@periodEnd": "2015-9-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.19"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q2",
                                "@periodEnd": "2015-12-31T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.26"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Actual",
                                "@period": "Q3",
                                "@periodEnd": "2015-3-31T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.34"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Estimate",
                                "@period": "Q4",
                                "@periodEnd": "2015-6-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$0.32"
                                }
                            }, {
                                "@displayRank": "850",
                                "@estimateActual": "Estimate",
                                "@period": "Annual",
                                "@periodEnd": "2015-6-30T00:00:00.00",
                                "@displayName": "EPS",
                                "CurrentValue": {
                                    "@displayValue": "$1.11"
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}
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)
    {
        if (source == null)
            return Enumerable.Empty<JToken>();
        IEnumerable<JToken> arr = source as JArray;
        return arr ?? new[] { source };
    }
}   
我得到以下错误:

'IEnumerable<JObject>' does not contain a definition for 'SelectToken' 
and no extension method 'SelectToken' accepting a first argument of 
type 'IEnumerable<JObject>' could be found
“IEnumerable”不包含“SelectToken”的定义
并且没有扩展方法“SelectToken”接受
找不到类型“IEnumerable”

我做错了什么?

我认为使用Json.Net可以很容易地做到这一点

给定以下json字符串

var json = @"
{
    ""?xml"": {
        ""@version"": ""1.0"",
        ""@encoding"": ""UTF-8""
    },
    ""DataFeed"": {
        ""@FeedName"": ""issuerDetails"",
        ""SecurityDetails"": {
            ""Security"": {
                ""@sequence"": ""850"",
                ""TimesSeriesList"": [{
                        ""@description"": ""EPS"",
                        ""@clientsDescription"": ""EPS"",
                        ""FinancialValue"": [{
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q1"",
                                ""@periodEnd"": ""2015-9-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.19""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q2"",
                                ""@periodEnd"": ""2015-12-31T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.26""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q3"",
                                ""@periodEnd"": ""2015-3-31T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.34""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Estimate"",
                                ""@period"": ""Q4"",
                                ""@periodEnd"": ""2015-6-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.32""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Estimate"",
                                ""@period"": ""Annual"",
                                ""@periodEnd"": ""2015-6-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$1.11""
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}";
以下内容似乎可以执行您想要的操作:

var obj = JObject.Parse(json);
obj["DataFeed"]["SecurityDetails"]["Security"]["TimesSeriesList"]
.Where(x => 
    x.HasValues 
    && x["@clientsDescription"].Value<string>() == "EPS")
.SelectMany(x => x["FinancialValue"].Children())
.Where(x => x["@estimateActual"].Value<string>() == "Actual")
.Select(x => new { 
    PeriodEnd = x["@periodEnd"].Value<DateTime>(), 
    Period = x["@period"].Value<string>(), 
    DisplayValue = x["CurrentValue"]["@displayValue"].Value<string>(),
    LastReportQuarter = x["@periodEnd"].Value<DateTime>().Year.ToString() + x["@period"].Value<string>()})
.OrderByDescending(x => x.PeriodEnd.Year)
.ThenByDescending(x => x.Period)
.First()
.Dump();
var obj=JObject.Parse(json); obj[“DataFeed”][“SecurityDetails”][“Security”][“TimeSeriesList”] .其中(x=> x、 有价值观 &&x[“@clientsdesdescription”].Value()=“EPS”) .SelectMany(x=>x[“FinancialValue”].Children() .其中(x=>x[“@estimateActual”].Value()==“实际”) .Select(x=>new{ PeriodEnd=x[“@PeriodEnd”].Value(), Period=x[“@Period”].Value(), DisplayValue=x[“CurrentValue”][“@DisplayValue”].Value(), LastReportQuarter=x[“@periodEnd”].Value().Year.ToString()+x[“@period”].Value()}) .OrderByDescending(x=>x.PeriodEnd.Year) .然后递减(x=>x.Period) .First() .Dump(); 期末2015年3月31日12:00:00 AM
第三季度
DisplayValue$0.34
2015年第三季度上一季度报告


我认为使用Json.Net可以很容易地做到这一点

给定以下json字符串

var json = @"
{
    ""?xml"": {
        ""@version"": ""1.0"",
        ""@encoding"": ""UTF-8""
    },
    ""DataFeed"": {
        ""@FeedName"": ""issuerDetails"",
        ""SecurityDetails"": {
            ""Security"": {
                ""@sequence"": ""850"",
                ""TimesSeriesList"": [{
                        ""@description"": ""EPS"",
                        ""@clientsDescription"": ""EPS"",
                        ""FinancialValue"": [{
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q1"",
                                ""@periodEnd"": ""2015-9-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.19""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q2"",
                                ""@periodEnd"": ""2015-12-31T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.26""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Actual"",
                                ""@period"": ""Q3"",
                                ""@periodEnd"": ""2015-3-31T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.34""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Estimate"",
                                ""@period"": ""Q4"",
                                ""@periodEnd"": ""2015-6-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$0.32""
                                }
                            }, {
                                ""@displayRank"": ""850"",
                                ""@estimateActual"": ""Estimate"",
                                ""@period"": ""Annual"",
                                ""@periodEnd"": ""2015-6-30T00:00:00.00"",
                                ""@displayName"": ""EPS"",
                                ""CurrentValue"": {
                                    ""@displayValue"": ""$1.11""
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}";
以下内容似乎可以执行您想要的操作:

var obj = JObject.Parse(json);
obj["DataFeed"]["SecurityDetails"]["Security"]["TimesSeriesList"]
.Where(x => 
    x.HasValues 
    && x["@clientsDescription"].Value<string>() == "EPS")
.SelectMany(x => x["FinancialValue"].Children())
.Where(x => x["@estimateActual"].Value<string>() == "Actual")
.Select(x => new { 
    PeriodEnd = x["@periodEnd"].Value<DateTime>(), 
    Period = x["@period"].Value<string>(), 
    DisplayValue = x["CurrentValue"]["@displayValue"].Value<string>(),
    LastReportQuarter = x["@periodEnd"].Value<DateTime>().Year.ToString() + x["@period"].Value<string>()})
.OrderByDescending(x => x.PeriodEnd.Year)
.ThenByDescending(x => x.Period)
.First()
.Dump();
var obj=JObject.Parse(json); obj[“DataFeed”][“SecurityDetails”][“Security”][“TimeSeriesList”] .其中(x=> x、 有价值观 &&x[“@clientsdesdescription”].Value()=“EPS”) .SelectMany(x=>x[“FinancialValue”].Children() .其中(x=>x[“@estimateActual”].Value()==“实际”) .Select(x=>new{ PeriodEnd=x[“@PeriodEnd”].Value(), Period=x[“@Period”].Value(), DisplayValue=x[“CurrentValue”][“@DisplayValue”].Value(), LastReportQuarter=x[“@periodEnd”].Value().Year.ToString()+x[“@period”].Value()}) .OrderByDescending(x=>x.PeriodEnd.Year) .然后递减(x=>x.Period) .First() .Dump(); 期末2015年3月31日12:00:00 AM
第三季度
DisplayValue$0.34
2015年第三季度上一季度报告


方法
ObjectsOrSelf
返回
IEnumerable
, 然后在子句
中,让a=data.SelectMany(t=>t.ObjectsOrSelf())
variable
a
具有IEnumerable类型,需要从a中的b进行扩展

var result = from securityDetail in obj.SelectTokens("DataFeed.SecurityDetails.Security.TimesSeriesList").SelectMany(t => t.ObjectsOrSelf())
    let metric = securityDetail.SelectToken("@clientsDescription")
    where metric != null && metric.Value<String>() == "EPS"
    let finValues = securityDetail.SelectTokens("FinancialValue").SelectMany(d => d.ObjectsOrSelf())
    from v in finValues
    orderby v.SelectToken("@periodEnd") descending
    orderby v.SelectToken("@period") descending
    select new
    {
        LastRptQtr = string.Format("{0}{1}",
            ((string) v.SelectToken("@periodEnd")).Substring(0, 4),
            (string) v.SelectToken("@period")),
        DispVal = v.SelectToken("CurrentValue.@displayValue").Value<String>()
    };
var result=from-securityDetail in-obj.SelectTokens(“DataFeed.SecurityDetails.Security.TimesSeriesList”).SelectMany(t=>t.ObjectsOrSelf())
让metric=securityDetail.SelectToken(“@clientsdesdescription”)
哪里是公制!=null&&metric.Value()=“EPS”
让finValues=securityDetail.SelectTokens(“FinancialValue”).SelectMany(d=>d.ObjectsOrSelf())
从v到finValues
orderby v.SelectToken(@periodEnd”)降序
orderby v.SelectToken(“@period”)降序
选择新的
{
LastRptQtr=string.Format(“{0}{1}”,
((字符串)v.SelectToken(@periodEnd”)。子字符串(0,4),
(字符串)v.SelectToken(“@period”),
DispVal=v.SelectToken(“CurrentValue@displayValue”).Value()
};

方法
ObjectsOrSelf
返回
IEnumerable
, 然后在子句
中,让a=data.SelectMany(t=>t.ObjectsOrSelf())
variable
a
具有IEnumerable类型,需要从a中的b进行扩展

var result = from securityDetail in obj.SelectTokens("DataFeed.SecurityDetails.Security.TimesSeriesList").SelectMany(t => t.ObjectsOrSelf())
    let metric = securityDetail.SelectToken("@clientsDescription")
    where metric != null && metric.Value<String>() == "EPS"
    let finValues = securityDetail.SelectTokens("FinancialValue").SelectMany(d => d.ObjectsOrSelf())
    from v in finValues
    orderby v.SelectToken("@periodEnd") descending
    orderby v.SelectToken("@period") descending
    select new
    {
        LastRptQtr = string.Format("{0}{1}",
            ((string) v.SelectToken("@periodEnd")).Substring(0, 4),
            (string) v.SelectToken("@period")),
        DispVal = v.SelectToken("CurrentValue.@displayValue").Value<String>()
    };
var result=from-securityDetail in-obj.SelectTokens(“DataFeed.SecurityDetails.Security.TimesSeriesList”).SelectMany(t=>t.ObjectsOrSelf())
让metric=securityDetail.SelectToken(“@clientsdesdescription”)
哪里是公制!=null&&metric.Value()=“EPS”
让finValues=securityDetail.SelectTokens(“FinancialValue”).SelectMany(d=>d.ObjectsOrSelf())
从v到finValues
orderby v.SelectToken(@periodEnd”)降序
orderby v.SelectToken(“@period”)降序
选择新的
{
LastRptQtr=string.Format(“{0}{1}”,
((字符串)v.SelectToken(@periodEnd”)。子字符串(0,4),
(字符串)v.SelectToken(“@period”),
DispVal=v.SelectToken(“CurrentValue@displayValue”).Value()
};
经典

如果你只有一把锤子,一切看起来都像钉子。您不需要将原始xml转换为json。处理原始xml会更简单

使用Linq2Xml+Xpath

var xDoc = XDocument.Parse(xmlstring);
var elements = xDoc.XPathSelectElements("//*[@periodEnd and (@clientsDescription='EPS' or @displayName='EPS')]")
                .OrderBy(x => (DateTime)x.Attribute("periodEnd"))
                .Select(x=>new {
                    PeriodEnd = (DateTime)x.Attribute("periodEnd"),
                    DispVal = (string)x.Element("CurrentValue").Attribute("displayValue")
                })
                .ToList();
经典的

如果你只有一把锤子,一切看起来都像钉子。您不需要将原始xml转换为json。处理原始xml会更简单

使用Linq2Xml+Xpath

var xDoc = XDocument.Parse(xmlstring);
var elements = xDoc.XPathSelectElements("//*[@periodEnd and (@clientsDescription='EPS' or @displayName='EPS')]")
                .OrderBy(x => (DateTime)x.Attribute("periodEnd"))
                .Select(x=>new {
                    PeriodEnd = (DateTime)x.Attribute("periodEnd"),
                    DispVal = (string)x.Element("CurrentValue").Attribute("displayValue")
                })
                .ToList();

原始xml是怎样的?使用方法ObjectsOrSelf可能更容易,它做什么?如何修复代码使其为我工作?原始xml如何?使用它可能更容易,方法ObjectsOrSelf做什么?如何修复代码使其为我工作?谢谢。使用原始XML似乎更快。谢谢。处理原始XML似乎更快。