获取三个最新时间的json元素

时间:2013-04-18 12:59:47

标签: json linq c#-4.0

我有一个json数组。元素格式可能如下:

{
    "CKey": "S2_123_45_20130416105655",
    "TimeOfCall": "2013-04-17T06:00:00-04:00",
    "DestinationNumber": "123456789",
    "CallType": "X",
    "CardNumber": ""
}

数组中有很多元素,我只想在24小时内按TimeOfCall(DateTime format)排序最新的三个元素。他们可能没什么,也可能是1,2。最大值为3 原文我想用几个字典按值存储和排序,但它太复杂了。

更新

这个json数组是来自C#代码的json字符串:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<CallDataDTO>));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(response4.Content.ReadAsStringAsync().Result));
calls = (List<CallDataDTO>)ser.ReadObject(ms); // dynamic
jsonStr4 = _dynamicConvert.DynamicToString(calls);// convert dynamic to string
jsonArray4 = JArray.Parse(jsonStr4); // convert string to a json array.

UPDATE2:

?jsonArray4
{[
   {
    "CKey": "P2_123_23_20130416105655",
    "TimeOfCall": "2013-04-17T06:00:00-04:00",
    "DestinationNumber": "1234567890",
    "CallType": "DEBIT",
    "CardNumber": ""
 },
 {
"CKey": "P5_123_105_20130412154035",
"TimeOfCall": "2013-04-17T07:00:00-04:00",
"DestinationNumber": "1234567890",
"CallType": "DEBIT",
"CardNumber": ""
},
{
  "CKey": "P5_123_114_20130412154000",
  "TimeOfCall": "2013-04-07T08:00:00-04:00",
  "DestinationNumber": "1234567890",
  "CallType": "DEBIT",
  "CardNumber": ""
}
]}
base {Newtonsoft.Json.Linq.JContainer}: {[
{
  "CKey": "P2_123_23_20130416105655",
  "TimeOfCall": "2013-04-17T06:00:00-04:00",
  "DestinationNumber": "1234567890",
  "CallType": "DEBIT",
  "CardNumber": ""
},
{
  "CKey": "P5_123_105_20130412154035",
  "TimeOfCall": "2013-04-17T07:00:00-04:00",
  "DestinationNumber": "1234567890",
  "CallType": "DEBIT",
  "CardNumber": ""
},
{
 "CKey": "P5_123_114_20130412154000",
 "TimeOfCall": "2013-04-07T08:00:00-04:00",
 "DestinationNumber": "1234567890",
 "CallType": "DEBIT",
 "CardNumber": ""
}
]}
 Type: Array

1 个答案:

答案 0 :(得分:1)

我会尝试像

这样的东西
jsonArray4.Where(item => Convert.ToDateTime(item["TimeOfCall"]).Date == DateTime.Now.Date)
.OrderByDescending(item => Convert.ToDateTime(item["TimeOfCall"]))
.Take(3);

更新:如果您需要过去24小时而不是当天:

jsonArray4.Where(item => Convert.ToDateTime(item["TimeOfCall"]) >= DateTime.Now.AddHours(-24))
.OrderByDescending(item => Convert.ToDateTime(item["TimeOfCall"]))
.Take(3);

Linq to Json

Update2:我之前建议的应该有效。这是我的完整代码示例:

string jsonText = @"[
{
    'CKey': 'P2_123_23_20130416105655',
    'TimeOfCall': '2013-04-17T06:00:00-04:00',
    'DestinationNumber': '1234567890',
    'CallType': 'DEBIT',
    'CardNumber': ''
},    
{
    'CKey': 'P5_123_105_20130412154035',
    'TimeOfCall': '2013-04-17T07:00:00-04:00',
    'DestinationNumber': '1234567890',
    'CallType': 'DEBIT',
    'CardNumber': ''
},
{
    'CKey': 'P5_123_114_20130412154000',
    'TimeOfCall': '2013-04-07T08:00:00-04:00',
    'DestinationNumber': '1234567890',
    'CallType': 'DEBIT',
    'CardNumber': ''
}    
]";

var result = JArray.Parse(jsonText).
    Where(item => Convert.ToDateTime(item["TimeOfCall"]).Date == new DateTime(2013, 4, 17))
    .OrderByDescending(item => Convert.ToDateTime(item["TimeOfCall"]))
    .Take(3);

请注意,我稍微改变了实际返回的条件。如果您需要过去24小时的结果,只需将条件更改为&gt; = DateTime.Now.AddHours(-24)