将一个json的DeserializeObject一个json放入字典中,并用PK和值作为ExecutionTime填充键

这是我从API接收的模型。

        var query = FindBy(item => item.TaskEnabledStatus == trueOrFalse).Select(items => new MainGridDto()
        {
            PK_Task = items.PK_Task,TaskName = items.TaskName,TaskDescription = items.TaskDescription,TaskEnabledStatus = items.TaskEnabledStatus,Businessname = items.tBusinessGroups.Businessname,ExecutionTime = items.ExecutionTime.Value,BusinessGroupDto = new BusinessGroupDto()
            {
                PK_BusinessGroup = items.tBusinessGroups.PK_BusinessGroup,BusinessGroupEnabledStatus = items.tBusinessGroups.BusinessGroupEnabledStatus
            },EmailBody = items.EmailBody,EmailSubject = items.EmailSubject,UserOwner = items.tUsers.tRecipients.FirstName + " " + items.tUsers.tRecipients.LastName,UsersDto = new UsersDto()
            {
                PK_User = items.tUsers.PK_User,username = items.tUsers.username,FirstAndLast = items.tUsers.tRecipients.FirstName + " " + items.tUsers.tRecipients.LastName
            },UserLastModified = items.tUsers.username,Frequency = items.Frequency,LastModifiedOn = items.LastModifiedOn
        });

我想做的是将对象反序列化为字典,但是由于键为空,所以我一直报错。如何将PK分配给字典并将ExecutionTime作为值。甚至为键和对象分配索引作为值?我使用过List,但是我想将其更改为字典,以提高搜索效率和性能。

var model = JsonConvert.DeserializeObject<Dictionary<int,ReportInfoDto>>(dtoResponse.Result);

一个附加说明,我正在使用linq查询搜索字典。那是好的做法还是有更有效的方法?

var itemsToSendOut = from reports in model
  where reports.Value.ExecutionTimeFormatted  == DateTime.Now.ToString("hh:mm tt ")
  select reports;

这就是我调用API并获得带有对象列表的json响应的方式。

private HttpClient client;
client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:57595/");
var getallReports = client.Getasync("api/AllReports").Result;
    if (getallReports.IsSuccessStatusCode)
                {

                    var dtoResponse = getallReports.Content.ReadAsStringAsync();
                    dtoResponse.Wait();
                    var model = JsonConvert.DeserializeObject<Dictionary<int,ReportInfoDto>>(dtoResponse.Result,new keyvaluepairConverter());

                    var itemsToSendOut = from reports in model
                                         where reports.Value.ExecutionTimeFormatted  == DateTime.Now.ToString("hh:mm tt ")
                                         select reports;

这是示例数据和确切格式,调试时结果给了我。

Result = "[{\"PK_Task\":3,\"TaskName\":\"ExampleReportName\",\"Businessname\":\"ExampleBusinessname\",\"UserOwner\":\"Stack Leet\",\"UsersDto\":{\"PK_User\":123,username\":\"Stack.L   ...
hbfym 回答:将一个json的DeserializeObject一个json放入字典中,并用PK和值作为ExecutionTime填充键

因此,在您的示例中,如果您在评论I am trying to check each report and look at their ExecutionTime中说的是对的,听起来像您想要的是:

Dictonary<DateTime,MyObject>

YourObject是您从API获得的List<>的单个实例。而您要做的就是从对象中提取ExecutionTime并将其用作字典中的键。

我说对了吗?我将在这个假设下回答。让我知道我是否错了。

据我所知,我认为没有办法将JSON字符串反序列化为Dictionary并指定哪个属性成为字典的键。 我可能错了!如果我是,我希望有人能告诉我并提供更好的答案。但是到目前为止,我还没有找到实现这一目标的方法。

话虽这么说,我该怎么做呢?

// Convert the json string from the API to a List:
List<MyObject> myObjects = JsonConvert.DeserializeObject<List<MyObject>>(dtoResponse.Result);

// Loop over list and make a Dictionary
Dictonary<DateTime,MyObject> model = new Dictonary<DateTime,MyObject>()
foreach (MyObject myObject in myObjects)
{
    if (myObject.ExecutionTime != null)
        model.Add(myObject.ExecutionTime,myObject);
}

再次, IF ,有一种方法可以在单个DeserializeObject<Dictionary...>行中完成所有操作,我很想知道这一点。我看了看,却没有看到任何地方的记录。但这将为您提供字典,然后您可以通过ExecutionTime轻松快速地进行搜索。

(作为一个补充说明:假设每个ExecutionTime是唯一的。如果不是,它们将不起作用。)

希望这会有所帮助。

编辑:

您问:

  

一个附加说明,我正在使用linq查询搜索字典。那是好的做法还是有更有效的方法?

var itemsToSendOut = from reports in model
  where reports.Value.ExecutionTimeFormatted  == DateTime.Now.ToString("hh:mm tt ")
  select reports;

用值中的某些内容搜索字典实际上会否定字典所提供的速度。如果这是您想要的,那么根本不需要字典。字典的速度之所以到来,是因为您可以通过键来搜索值。因此,您可以进行O(1)搜索。而如果您要搜索列表(或按其值来查找字典),则需要进行O(n)搜索,其中n是列表或词典中的项数。

本文链接:https://www.f2er.com/2977063.html

大家都在问