LINQ on polymorphic json 获取逗号分隔字段
LINQ on polymorphic json to get a comma separated field
我有一个多态 json 字符串。这是它的样子:
{
"?xml" : {
"@version" : "1.0",
"@encoding" : "UTF-8"
},
"DataFeed" : {
"@FeedName" : "complianceCompany",
"Companies" : {
"@timeStamp" : "2016-07-06T10:16:51.00",
"Company" : [{
"@id" : "1",
"Ticker" : "MSFT-NSDQ",
"Name" : "Microsoft",
"Disclosure" : {
"@Code" : "25",
"@Description" : "25: PERSON FINANCIAL INTEREST"
}
}, {
"@id" : "3",
"Ticker" : "AAPL-NSDQ",
"Name" : "Apple",
"Disclosure" : [{
"@Code" : "3",
"@Description" : "03: RECEIVED COMP FOR NON-IB SVS"
}, {
"@Code" : "6C",
"@Description" : "06C: CLIENT NON SEC REL SVS"
}, {
"@Code" : "39",
"@Description" : "39: MARKET MAKER"
}
]
}
]
}
}
}
曾经有人帮我做了以下扩展 class:
public static class JsonExtensions
{
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;
}
}
基于此,这是我的查询:
JObject feed = JObject.Parse(jsonText);
var discInfo = from issuer in feed.SelectTokens("DataFeed.Companies.Company").SelectMany(i => i.ObjectsOrSelf())
let discs = issuer.SelectTokens("Disclosure").SelectMany(s => s.ObjectsOrSelf())
select new
{
Id = (int)issuer["@id"],
Ticker = (string)issuer["Ticker"],
CompName = (string)issuer["Name"],
DiscCode = (string)discs["@Code"],
};
当我尝试查询时,出现以下错误:
Cannot apply indexing with [] to an expression of type 'IEnumerable<JObject>'
我正在尝试在逗号分隔的字段中为每个代码获取所有光盘。最终结果应如下所示:
如何获得最终结果?
当你做...
DiscCode = (string)discs["@Code"]
...您正在尝试访问披露 JObject
的 属性 "@Code"
。但是 discs
是 IEnumerable<JObject>
类型的!您必须收集 discs
中的所有 "@Code"
并将它们连接起来。
LINQ 的 Aggregate()
can do this, but I think it always looks more readable to use string.Join()
为此:
DiscCode = string.Join(", ", discs.Select(d => (string)d["@Code"]))
我有一个多态 json 字符串。这是它的样子:
{
"?xml" : {
"@version" : "1.0",
"@encoding" : "UTF-8"
},
"DataFeed" : {
"@FeedName" : "complianceCompany",
"Companies" : {
"@timeStamp" : "2016-07-06T10:16:51.00",
"Company" : [{
"@id" : "1",
"Ticker" : "MSFT-NSDQ",
"Name" : "Microsoft",
"Disclosure" : {
"@Code" : "25",
"@Description" : "25: PERSON FINANCIAL INTEREST"
}
}, {
"@id" : "3",
"Ticker" : "AAPL-NSDQ",
"Name" : "Apple",
"Disclosure" : [{
"@Code" : "3",
"@Description" : "03: RECEIVED COMP FOR NON-IB SVS"
}, {
"@Code" : "6C",
"@Description" : "06C: CLIENT NON SEC REL SVS"
}, {
"@Code" : "39",
"@Description" : "39: MARKET MAKER"
}
]
}
]
}
}
}
曾经有人帮我做了以下扩展 class:
public static class JsonExtensions
{
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;
}
}
基于此,这是我的查询:
JObject feed = JObject.Parse(jsonText);
var discInfo = from issuer in feed.SelectTokens("DataFeed.Companies.Company").SelectMany(i => i.ObjectsOrSelf())
let discs = issuer.SelectTokens("Disclosure").SelectMany(s => s.ObjectsOrSelf())
select new
{
Id = (int)issuer["@id"],
Ticker = (string)issuer["Ticker"],
CompName = (string)issuer["Name"],
DiscCode = (string)discs["@Code"],
};
当我尝试查询时,出现以下错误:
Cannot apply indexing with [] to an expression of type 'IEnumerable<JObject>'
我正在尝试在逗号分隔的字段中为每个代码获取所有光盘。最终结果应如下所示:
如何获得最终结果?
当你做...
DiscCode = (string)discs["@Code"]
...您正在尝试访问披露 JObject
的 属性 "@Code"
。但是 discs
是 IEnumerable<JObject>
类型的!您必须收集 discs
中的所有 "@Code"
并将它们连接起来。
LINQ 的 Aggregate()
can do this, but I think it always looks more readable to use string.Join()
为此:
DiscCode = string.Join(", ", discs.Select(d => (string)d["@Code"]))