1

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

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

数组中有很多元素,我只想要TimeOfCall(DateTime format)24 小时内的最新三个订单。它们可能什么都不是,也可能是 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.

更新2:

?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
4

1 回答 1

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 到 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 小时的结果,则将条件更改为 >= DateTime.Now.AddHours(-24)

于 2013-04-18T13:48:38.493 回答