【发布时间】:2015-03-18 10:55:18
【问题描述】:
我正在尝试为 JToken 上的所有子节点的属性提取日期列表,但无法正确获取语法。
我想获取属性 "timeStamp": "2013-09-11T00:30:00Z" 中的日期列表,以便确定所有子节点的最小/最大日期。
我尝试了以下方法,它返回一个匿名类型,并且很难使用返回的对象。
var timeStamps = Jarr.Select(x => new
{
timeStamp = (DateTime)x.SelectToken("timeStamp")
});
- 我怎样才能只说所有子时间戳中的
List<string>或List<DateTime>? - 是否可以得到
Dictionary<string, DateTime>的id、时间戳?
Json 看起来像这样,所以基本上从 LEVEL1 我想检查所有孩子,孩子的孩子是否有相同的属性。
{
"children": [
{
"type": "LEVEL2",
"name": "Item1",
"id": "1.7193",
"timeStamp": "2013-09-11T00:30:00Z",
},
{
"type": "LEVEL2",
"name": "Item2",
"id": "1.7194",
"timeStamp": "2013-09-11T00:30:00Z",
},
{
"type": "LEVEL2",
"name": "Item3",
"id": "1.7191",
"timeStamp": "2013-09-11T00:30:00Z",
}
],
"type": "LEVEL1",
"name": "Stock-FRT54443",
"id": "1000145",
"countryCode": "en"
}
和方法
void AddNodes(TreeView treeView, JObject jObj, TreeNodeCollection parent)
{
JToken Jarr = null;
Dictionary<string, string> marketProperties = new Dictionary<string, string>();
foreach (var property in jObj.Properties())
{
if (property.Name == "children")
{
Jarr = property.Value;
}
else
{
string key = property.Name;
string prop = property.Value.ToString();
marketProperties.Add(key, prop);
}
}
if (marketProperties["type"] == "LEVEL1")
{
//Not working!
var timeStamps = Jarr["timeStamp"].Values<string>();
}
}
【问题讨论】: