我正在尝试获取 SEDOL 和 ADP 值的列表。下面是我的 json 文本:
{
"DataFeed" : {
"@FeedName" : "AdminData",
"Issuer" : [{
"id" : "1528",
"name" : "ZYZ.A a Test Company",
"clientCode" : "ZYZ.A",
"securities" : {
"Security" : {
"id" : "1537",
"sedol" : "SEDOL111",
"coverage" : {
"Coverage" : [{
"analyst" : {
"@id" : "164",
"@clientCode" : "SJ",
"@firstName" : "Steve",
"@lastName" : "Jobs",
"@rank" : "1"
}
}, {
"analyst" : {
"@id" : "261",
"@clientCode" : "BG",
"@firstName" : "Bill",
"@lastName" : "Gates",
"@rank" : "2"
}
}
]
},
"customFields" : {
"customField" : [{
"@name" : "ADP Security Code",
"@type" : "Textbox",
"values" : {
"value" : "ADPSC1111"
}
}, {
"@name" : "Top 10 - Select one or many",
"@type" : "Dropdown, multiple choice",
"values" : {
"value" : ["Large Cap", "Cdn Small Cap", "Income"]
}
}
]
}
}
}
}, {
"id" : "1519",
"name" : "ZVV Test",
"clientCode" : "ZVV=US",
"securities" : {
"Security" : [{
"id" : "1522",
"sedol" : "SEDOL112",
"coverage" : {
"Coverage" : {
"analyst" : {
"@id" : "79",
"@clientCode" : "MJ",
"@firstName" : "Michael",
"@lastName" : "Jordan",
"@rank" : "1"
}
}
},
"customFields" : {
"customField" : [{
"@name" : "ADP Security Code",
"@type" : "Textbox",
"values" : {
"value" : "ADPS1133"
}
}, {
"@name" : "Top 10 - Select one or many",
"@type" : "Dropdown, multiple choice",
"values" : {
"value" : ["Large Cap", "Cdn Small Cap", "Income"]
}
}
]
}
}, {
"id" : "1542",
"sedol" : "SEDOL112",
"customFields" : {
"customField" : [{
"@name" : "ADP Security Code",
"@type" : "Textbox",
"values" : {
"value" : "ADPS1133"
}
}, {
"@name" : "Top 10 - Select one or many",
"@type" : "Dropdown, multiple choice",
"values" : {
"value" : ["Large Cap", "Cdn Small Cap", "Income"]
}
}
]
}
}
]
}
}
]
}
}
这是我到目前为止的代码:
var compInfo = feed["DataFeed"]["Issuer"]
.Select(p => new {
Id = p["id"],
CompName = p["name"],
SEDOL = p["securities"]["Security"].OfType<JArray>() ?
p["securities"]["Security"][0]["sedol"] :
p["securities"]["Security"]["sedol"]
ADP = p["securities"]["Security"].OfType<JArray>() ?
p["securities"]["Security"][0]["customFields"]["customField"][0]["values"]["value"] :
p["securities"]["Security"]["customFields"]["customField"][0]["values"]["value"]
});
我得到的错误是:
使用无效键值访问的 JArray 值:“sedol”。需要 Int32 数组索引
我想我真的很接近弄清楚这一点。我应该怎么做才能修复代码?如果有其他方法可以获取SEDOL
and ADP value
,请告诉我?
[UPDATE1]我已经开始使用动态 ExpandoObject。这是我到目前为止使用的代码:
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, new ExpandoObjectConverter());
foreach (dynamic element in obj)
{
Console.WriteLine(element.DataFeed.Issuer[0].id);
Console.WriteLine(element.DataFeed.Issuer[0].securities.Security.sedol);
Console.ReadLine();
}
但我现在得到了错误'ExpandoObject' does not contain a definition for 'DataFeed' and no extension method 'DataFeed' accepting a first argument of type 'ExpandoObject' could be found
。 注意:我了解此 json 文本格式错误。一个实例有一个数组,另一个是一个对象。我希望代码足够敏捷以处理这两个实例。
[UPDATE2]感谢@dbc 到目前为止帮助我编写代码。我已经更新了上面的 json 文本以紧密匹配我当前的环境。我现在可以获得 SEDOL 和 ADP 代码。但是,当我尝试获得第一个分析师时,我的代码仅适用于对象并为作为数组一部分的分析师生成空值。这是我当前的代码:
var compInfo = from issuer in feed.SelectTokens("DataFeed.Issuer").SelectMany(i => i.ObjectsOrSelf())
let security = issuer.SelectTokens("securities.Security").SelectMany(s => s.ObjectsOrSelf()).FirstOrDefault()
where security != null
select new
{
Id = (string)issuer["id"], // Change to (string)issuer["id"] if id is not necessarily numeric.
CompName = (string)issuer["name"],
SEDOL = (string)security["sedol"],
ADP = security["customFields"]
.DescendantsAndSelf()
.OfType<JObject>()
.Where(o => (string)o["@name"] == "ADP Security Code")
.Select(o => (string)o.SelectToken("values.value"))
.FirstOrDefault(),
Analyst = security["coverage"]
.DescendantsAndSelf()
.OfType<JObject>()
.Select(jo => (string)jo.SelectToken("Coverage.analyst.@lastName"))
.FirstOrDefault(),
};
我需要更改哪些内容才能始终选择第一个分析师?