下面是我擁有的json字符串。LINQ to 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"
}
]
}
]
}
]
}
}
}
}
}
如何選擇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][email protected]"),
FinModelYear2015 = (string)finModels.SelectToken("Values[1].Value[1][email protected]"),
FinModelValue2015 = (string)finModels.SelectToken("Values[1].Value[1].#text"),
FinModelYear2016 = (string)finModels.SelectToken("Values[1].Value[2][email protected]"),
FinModelValue2016 = (string)finModels.SelectToken("Values[1].Value[2].#text"),
FinModelYear2017 = (string)finModels.SelectToken("Values[1].Value[3][email protected]"),
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方法。你會好心幫我完成我的查詢嗎?
NOTE:我正在下載一個XML字符串,將它轉換爲JSON然後解析它。
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
jsonText = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
JObject jsonFeed = JObject.Parse(jsonText);
我寧願用我已經使用的方法。正如我所提到的,JSON將是動態的。因此,下一個對象的對象屬性可能不是靜態的。我們不能使用已經提到的某種方法嗎? –