如何正确执行 JSON API GET 请求并分配输出Kimai 时间跟踪

问题描述

我想编写一个简单的桌面应用程序来跟踪我们员工的加班情况。每当我们的一名员工需要在正常工作时间之外执行某些任务时,我们都希望使用“Kimai 时间跟踪”对其进行跟踪。

我正在编写的应用程序需要从 Kimai 获取所有记录任务的持续时间,将它们相加并存储在本地 sql Server 上,作为员工日后申请的加班条件。

这是我主要使用的 GET 请求:

GET /api/timesheets(返回时间表记录的集合)

GET /api/timesheets

这是来自 Kimai Demo 的示例输出(抱歉篇幅过长)

[
  {
    "activity": 174,"project": 12,"user": 1,"tags": [],"id": 5610,"begin": "2021-05-28T01:44:00-0700","end": null,"duration": null,"description": null,"rate": 0,"internalRate": 0,"exported": false,"billable": true,"MetaFields": []
  },{
    "activity": 305,"project": 23,"tags": [
      "Et quo.","Aut ut."
    ],"id": 240,"begin": "2021-05-25T23:33:00-0700","end": "2021-05-26T05:18:00-0700","duration": 20700,"description": "Latitude was,or Longitude either,but thought they were mine before. If I or she should chance to be patted on the.","rate": 465.75,{
    "activity": 486,"project": 40,"id": 155,"begin": "2021-05-23T14:48:00-0700","end": "2021-05-23T15:21:00-0700","duration": 1980,"rate": 44.55,{
    "activity": 667,"project": 52,"tags": [
      "MistyRose"
    ],"id": 300,"begin": "2021-05-20T20:27:00-0700","end": "2021-05-21T08:10:00-0700","duration": 42180,"description": "march Hare went on. 'Or would you like the wind,and the cool fountains. CHAPTER VIII. The Queen's.","rate": 949.05,{
    "activity": 789,"project": 62,"id": 272,"begin": "2021-05-19T01:21:00-0700","end": "2021-05-19T12:47:00-0700","duration": 41160,"rate": 926.1,{
    "activity": 585,"project": 45,"tags": [
      "in","Orphaborough"
    ],"id": 120,"begin": "2021-05-17T12:48:00-0700","end": "2021-05-17T18:56:00-0700","duration": 22080,"description": "Who ever saw in another moment that it was very like having a game of play with a deep voice,'are done.","rate": 496.8,{
    "activity": 250,"project": 19,"tags": [
      "Blue","Vergiebury"
    ],"id": 68,"begin": "2021-05-17T02:40:00-0700","end": "2021-05-17T05:21:00-0700","duration": 9660,"rate": 217.35,{
    "activity": 426,"project": 33,"tags": [
      "Tomato"
    ],"id": 5609,"begin": "2021-05-12T05:50:00-0700","end": "2021-05-12T06:21:00-0700","duration": 1860,"rate": 41.85,"internalRate": 41.85,{
    "activity": 62,"project": 6,"tags": [
      "Port Jeremiefort","Gonzalo Summit"
    ],"id": 327,"begin": "2021-05-09T04:13:00-0700","end": "2021-05-09T07:08:00-0700","duration": 10500,"description": "Duchess's cook. She carried the pepper-Box in her hands.","rate": 236.25,{
    "activity": 465,"project": 36,"id": 321,"begin": "2021-05-08T08:38:00-0700","end": "2021-05-08T22:37:00-0700","duration": 50340,"description": "I grow up,I'll write one--but I'm grown up Now,' she added in an undertone.","rate": 1132.65,{
    "activity": 1155,"project": 92,"tags": [
      "Chocolate"
    ],"id": 122,"begin": "2021-05-03T16:20:00-0700","end": "2021-05-03T19:49:00-0700","duration": 12540,"rate": 282.15,{
    "activity": 916,"project": 73,"tags": [
      "Schiller brooks"
    ],"id": 215,"begin": "2021-04-29T21:32:00-0700","end": "2021-04-30T00:36:00-0700","duration": 11040,"rate": 248.4,"id": 5608,"begin": "2021-04-27T05:50:00-0700","end": "2021-04-27T06:21:00-0700",{
    "activity": 175,"tags": [
      "itaque","Harberland"
    ],"id": 253,"begin": "2021-04-25T18:18:00-0700","end": "2021-04-26T00:40:00-0700","duration": 22920,"rate": 515.7,{
    "activity": 439,"tags": [
      "Fay Stravenue"
    ],"id": 40,"begin": "2021-04-20T09:07:00-0700","end": "2021-04-20T11:09:00-0700","duration": 7320,"rate": 164.7,{
    "activity": 903,"tags": [
      "South Maraview"
    ],"id": 55,"begin": "2021-04-12T10:00:00-0700","end": "2021-04-12T19:57:00-0700","duration": 35820,"rate": 805.95,{
    "activity": 406,"project": 32,"id": 334,"begin": "2021-04-06T09:08:00-0700","end": "2021-04-06T10:04:00-0700","duration": 3360,"rate": 75.6,{
    "activity": 799,"project": 63,"tags": [
      "Morar Plaza"
    ],"id": 312,"begin": "2021-04-03T18:57:00-0700","end": "2021-04-04T08:47:00-0700","duration": 49800,"description": "Yet you turned a back-somersault in at the house,\"Let us both go to on the breeze that followed them,the melancholy words:-- 'Soo--oop of the trees.","rate": 1120.5,{
    "activity": 119,"project": 10,"tags": [
      "debitis"
    ],"id": 203,"begin": "2021-03-30T09:59:00-0700","end": "2021-03-30T14:31:00-0700","duration": 16320,"description": "Quam possimus earum ut tenetur eligendi. Autem est possimus at hic. Eius voluptatem dolores nihil qui debitis et laudantium.","rate": 367.2,{
    "activity": 118,"id": 159,"begin": "2021-03-30T02:06:00-0700","end": "2021-03-30T04:25:00-0700","duration": 8340,"description": "I'll never go THERE again!' said Alice very politely; but she felt certain it must be what he did with the time,' she said,'than waste it in with a sigh.","rate": 187.65,{
    "activity": 259,"project": 20,"id": 294,"begin": "2021-03-29T20:33:00-0700","end": "2021-03-30T06:01:00-0700","duration": 34080,"description": "When the pie was all dark overhead; before her was another long passage,and the beak-- Pray how did you ever eat a little scream,half of them--and it belongs to the Mock.","rate": 766.8,{
    "activity": 108,"project": 9,"id": 41,"begin": "2021-03-26T17:22:00-0700","end": "2021-03-26T19:48:00-0700","duration": 8760,"rate": 197.1,{
    "activity": 1022,"project": 79,"id": 222,"begin": "2021-03-25T08:40:00-0700","end": "2021-03-25T19:38:00-0700","duration": 39480,"description": "I can find them.' As she said this,she looked up eagerly,half hoping that the Mouse heard this,it turned a back-somersault in at the.","rate": 888.3,{
    "activity": 387,"project": 31,"id": 198,"begin": "2021-03-24T19:17:00-0700","end": "2021-03-24T21:32:00-0700","duration": 8100,"description": "I to get us dry would be only rustling in the distance. 'And yet.","rate": 182.25,{
    "activity": 1082,"project": 88,"id": 213,"begin": "2021-03-23T09:35:00-0700","end": "2021-03-23T19:09:00-0700","duration": 34440,"description": "Mock Turtle in a tone of great surprise. 'Of course it is,' said the Queen,the royal children,and make out which were the verses the White Rabbit hurried by--the frightened.","rate": 774.9,{
    "activity": 683,"project": 53,"tags": [
      "Windler Springs","Lulu Mount"
    ],"id": 146,"begin": "2021-03-17T11:20:00-0700","end": "2021-03-17T16:01:00-0700","duration": 16860,"rate": 379.35,{
    "activity": 984,"project": 77,"id": 36,"begin": "2021-03-09T07:24:00-0800","end": "2021-03-09T10:33:00-0800","duration": 11340,"description": "Who Stole the Tarts? The King and Queen of Hearts,who only bowed and smiled in reply. 'Idiot!' said the Hatter,it woke up.","rate": 255.15,{
    "activity": 265,"id": 138,"begin": "2021-02-25T08:17:00-0800","end": "2021-02-25T13:09:00-0800","duration": 17520,"description": "Alice; 'you needn't be afraid of them!' 'And who are THESE?' said the Mock Turtle with a teacup in one hand and a sad tale!' said the Mock Turtle sighed deeply,and drew the back of one flapper.","rate": 394.2,{
    "activity": 638,"project": 50,"tags": [
      "Keonberg"
    ],"id": 282,"begin": "2021-02-23T12:18:00-0800","end": "2021-02-23T17:13:00-0800","duration": 17700,"description": "You gave us three or more; They.","rate": 398.25,{
    "activity": 753,"project": 58,"id": 100,"begin": "2021-02-22T14:14:00-0800","end": "2021-02-22T19:11:00-0800","duration": 17820,"rate": 400.95,{
    "activity": 1105,"project": 89,"tags": [
      "Ea iste."
    ],"id": 98,"begin": "2021-02-22T08:30:00-0800","end": "2021-02-22T19:17:00-0800","duration": 38820,"description": "Mollitia optio at quidem ut SAEpe impedit consequatur. Repellat id et labore inventore magnam. Animi ex necessitatibus magni ut.","rate": 873.45,{
    "activity": 735,"project": 57,"id": 232,"begin": "2021-02-22T04:42:00-0800","end": "2021-02-22T17:24:00-0800","duration": 45720,"rate": 1028.7,{
    "activity": 445,"project": 34,"tags": [
      "Hegmann Squares","Camylle Drive"
    ],"id": 148,"begin": "2021-02-21T09:57:00-0800","end": "2021-02-21T23:53:00-0800","duration": 50160,"rate": 1128.6,{
    "activity": 302,"id": 73,"begin": "2021-02-20T16:03:00-0800","end": "2021-02-21T03:56:00-0800","duration": 42780,"rate": 962.55,"id": 320,"begin": "2021-02-16T09:25:00-0800","end": "2021-02-16T11:55:00-0800","duration": 9000,"rate": 202.5,{
    "activity": 896,"project": 72,"id": 27,"begin": "2021-02-13T04:40:00-0800","end": "2021-02-13T14:56:00-0800","duration": 36960,"description": "Bill's place for a minute or two,she made it out to sea. So they got their tails in their mouths; and the reason they're called lessons,' the Gryphon.","rate": 831.6,"tags": [
      "Nobis sit.","Sed iure."
    ],"id": 2,"begin": "2021-02-13T00:22:00-0800","end": "2021-02-13T14:08:00-0800","duration": 49560,"rate": 1115.1,{
    "activity": 938,"project": 74,"tags": [
      "Clementina Locks"
    ],"id": 343,"begin": "2021-02-12T08:38:00-0800","end": "2021-02-12T16:40:00-0800","duration": 28920,"description": "Laboriosam dolorem laboriosam fugit qui quidem perferendis voluptas. Provident repudiandae maiores quia quis.","rate": 650.7,{
    "activity": 47,"project": 3,"tags": [
      "Bashirianfort"
    ],"id": 60,"begin": "2021-02-11T11:21:00-0800","end": "2021-02-11T14:08:00-0800","duration": 10020,"description": "I should.","rate": 225.45,{
    "activity": 37,"tags": [
      "north Vanville","Tromp Lights"
    ],"id": 72,"begin": "2021-02-08T21:58:00-0800","end": "2021-02-09T11:57:00-0800","description": "Queen,and Alice,were in custody and under sentence of.",{
    "activity": 13,"project": 1,"id": 190,"begin": "2021-02-05T18:50:00-0800","end": "2021-02-05T23:09:00-0800","duration": 15540,"rate": 349.65,{
    "activity": 1071,"project": 86,"id": 200,"begin": "2021-01-30T23:04:00-0800","end": "2021-01-31T00:45:00-0800","duration": 6060,"rate": 136.35,{
    "activity": 940,"tags": [
      "Sienna"
    ],"id": 14,"begin": "2021-01-27T19:52:00-0800","end": "2021-01-27T23:47:00-0800","duration": 14100,"description": "Corrupti suscipit sequi fugiat placeat ab. Impedit magnam et voluptatem iste eligendi temporibus. Consequuntur quaerat maxime aut provident odit quis.","rate": 317.25,{
    "activity": 806,"project": 64,"tags": [
      "quibusdam"
    ],"id": 238,"begin": "2021-01-26T14:15:00-0800","end": "2021-01-26T20:41:00-0800","duration": 23160,"description": "Quia et reprehenderit tempora sit tempora. Aliquam numquam ad est omnis exercitationem. Et architecto itaque quas omnis. Numquam at et reprehenderit hic expedita consectetur at atque.","rate": 521.1,{
    "activity": 715,"project": 55,"tags": [
      "Whitechester","Schmitt Square"
    ],"id": 188,"begin": "2021-01-23T14:24:00-0800","end": "2021-01-24T02:47:00-0800","duration": 44580,"rate": 1003.05,{
    "activity": 626,"project": 49,"tags": [
      "McCullough Spring","New Vidaburgh"
    ],"id": 77,"begin": "2021-01-22T10:58:00-0800","end": "2021-01-22T19:27:00-0800","duration": 30540,"description": "Nisi sit est veritatis quae amet quod. Atque iure ratione animi iste. Quisquam et ad assumenda aut. Ut tempora itaque facere voluptas similique ab. Est est qui libero et aliquam nihil.","rate": 687.15,{
    "activity": 632,"id": 65,"begin": "2021-01-18T12:24:00-0800","end": "2021-01-18T22:22:00-0800","duration": 35880,"rate": 807.3,{
    "activity": 497,"project": 41,"tags": [
      "Quis.","tempora"
    ],"id": 332,"begin": "2021-01-15T00:13:00-0800","end": "2021-01-15T02:38:00-0800","duration": 8700,"rate": 195.75,{
    "activity": 765,"id": 287,"begin": "2021-01-13T10:49:00-0800","end": "2021-01-13T15:58:00-0800","duration": 18540,"description": "Hic facilis a ipsa maxime aliquid enim. In aliquam et sequi soluta. Unde quis sunt ducimus officiis commodi.","rate": 417.15,{
    "activity": 151,"project": 11,"tags": [
      "Adrien Union"
    ],"id": 80,"begin": "2021-01-04T15:08:00-0800","end": "2021-01-04T19:54:00-0800","duration": 17160,"rate": 386.1,"MetaFields": []
  }
]

我只对持续时间值感兴趣。我需要将它们全部加在一起并将它们存储为一个整数变量(写入 sql DB)。

所以我的问题是:这样做的最佳实践方法是什么?我试过 Process.Start() 并将 StandardOutput 重定向到字符串变量。这在技术上可行,但将所有“持续时间值”等分开会很痛苦。

有没有更好的方法来做到这一点?

提前致谢!


如果你想自己测试:

Kimai 现场演示:https://demo.kimai.org

演示登录凭据:https://www.kimai.org/demo

Kimai API:https://demo.kimai.org/api/doc登录后)

解决方法

您可以使用 HttpClient API 发出 REST 请求,然后直接在您的 .NET 应用程序中解析响应:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-AUTH-USER","susan_super");
httpClient.DefaultRequestHeaders.Add("X-AUTH-TOKEN","api_kitten");
string json = await httpClient.GetStringAsync("https://demo.kimai.org/api/timesheets?user=1");

无需启动另一个进程来执行此操作。

How can I parse JSON with C#?