这是我拥有的 json 字符串。
{
"?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",
"@currency" : "C$",
"Value" : [{
"@year" : "2014",
"#text" : "0"
}, {
"@year" : "2015",
"#text" : "-1.667"
}, {
"@year" : "2016",
"#text" : "-1.212"
}, {
"@year" : "2017",
"#text" : "0.202"
}, {
"@year" : "2018",
"#text" : "0"
}
]
}, {
"@name" : "CFPS",
"@clientCode" : "CFPS",
"@currency" : "C$",
"Value" : [{
"@year" : "2014",
"#text" : "3.196"
}, {
"@year" : "2015",
"#text" : "-0.207"
}, {
"@year" : "2016",
"#text" : "0.599"
}, {
"@year" : "2017",
"#text" : "2.408"
}, {
"@year" : "2018",
"#text" : "0"
}
]
}
]
}
]
}
}
}
}
}
如何选择#text
2015、2016、2017 年的 EPS 数据?这是我到目前为止的查询:
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);