【发布时间】:2021-06-01 09:30:14
【问题描述】:
我有一个 JSON
{
"departments": [
{
"2": {"city": "Petersburg", "employees": "1200"}
},
{
"1": {"city": "Ekaterinburg", "employees": "4000"}
}
]
}
如果我知道使用 LINQ 或其他方法的 ID,我如何获得城市的价值?
我试过了
var id = 2;
json["departments"].Single(x=>x.Name==id.ToString())["city"];
但它不起作用,我得到一个编译错误:
'JToken' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'JToken' could be found (are you missing a using directive or an assembly reference?)
演示小提琴here.
【问题讨论】: