将 Zendesk 工单获取到 SQL(JSON 处理)中

本文关键字:SQL JSON 处理 获取 Zendesk 单获取 | 更新日期: 2023-09-27 18:17:25

在 Zendesk 开发者博客中,我找到了一个有用的条目(https://developer.zendesk.com/blog/getting-and-createating-tickets-with-.net(。

我在 C# 控制台应用程序中使用了文章中的代码进行测试:

using System;
using ConsoleApplication1.Properties;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators;
namespace ConsoleApplication1
{
    public class Ticket
    {
        public int id { get; set; }
        public string url { get; set; }
        public string external_id { get; set; }
        public string type { get; set; }
        public string subject { get; set; }
        public string description { get; set; }
        public string priority { get; set; }
        public string status { get; set; }
        public string recipient { get; set; }
        public int requester_id { get; set; }
        public int submitter_id { get; set; }
        public int assignee_id { get; set; }
        public int organization_id { get; set; }
        public int group_id { get; set; }
    }
    internal class Program
    {
        private static readonly string requestUri = "https://xxx.zendesk.com/";
        private static readonly string username = Settings.Default.Username;
        private static readonly string password = Settings.Default.Password;
        private static void Main(string[] args)
        {
            var client = new RestClient(requestUri);
            client.Authenticator = new HttpBasicAuthenticator(username, password);
            var request = new RestRequest("/api/v2/tickets.json", Method.GET);
            client.AddDefaultHeader("Accept", "application/json");
            var response = client.Execute(request);
            var content = response.Content;
            var ticket = JObject.Parse(content);
            foreach (var pair in ticket)
            {
                Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
            }
        }
    }
}

它有效,我收到以下数据:

tickets: [
  {
    "url": "xxx.zendesk.com/api/v2/tickets/1.json",
    "id": 1,
    "external_id": null,
    "via": {
      "channel": "sample_ticket",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2015-10-12T12:08:25Z",
    "updated_at": "2015-10-12T18:15:13Z",
    "type": "incident",
    "subject": "Beispielticket: erster Blick auf das Ticket",
    "raw_subject": "Beispielticket: erster Blick auf das Ticket",
    "description": "Hallo xxx,'n'nE-Mails, Chats, Voicemails und Tweets werden in Zendesk in Form von Tickets erfasst. Geben Sie oben Ihre Antwort
ein und klicken Sie dann auf "Einreichen", um sie zu senden. Um auszuprobieren, wie eine E-Mail zu einem Ticket wird, senden Sie eine Nachricht an sup
port@xxx.zendesk.com.'n",
    "priority": "normal",
    "status": "open",
    "recipient": null,
    "requester_id": 1371652071,
    "submitter_id": 1371651941,
    "assignee_id": null,
    "organization_id": null,
    "group_id": 24976891,
    "collaborator_ids": [],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "due_at": null,
    "tags": [
      "zendesk-beispiel"
    ],
    "custom_fields": [
      {
        "id": 28089621,
        "value": null
      },
      {
        "id": 28087191,
        "value": null
      },
      {
        "id": 28087201,
        "value": null
      }
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [],
    "fields": [
      {
        "id": 28089621,
        "value": null
      },
      {
        "id": 28087191,
        "value": null
      },
      {
        "id": 28087201,
        "value": null
      }
    ],
    "brand_id": 782281
  },
  {
    "url": "xxx.zendesk.com/api/v2/tickets/2.json",
    "id": 2,
    "external_id": null,
    "via": {
      "channel": "web",
      "source": {
        "from": {},
        "to": {},
        "rel": null
      }
    },
    "created_at": "2015-10-12T20:26:13Z",
    "updated_at": "2015-10-12T20:26:13Z",
    "type": "question",
    "subject": "test",
    "raw_subject": "test",
    "description": "test 1234",
    "priority": "normal",
    "status": "open",
    "recipient": null,
    "requester_id": 1371651941,
    "submitter_id": 1371651941,
    "assignee_id": 1371651941,
    "organization_id": 157600651,
    "group_id": 24976891,
    "collaborator_ids": [],
    "forum_topic_id": null,
    "problem_id": null,
    "has_incidents": false,
    "due_at": null,
    "tags": [
      "jpa"
    ],
    "custom_fields": [
      {
        "id": 28089621,
        "value": "jpa"
      },
      {
        "id": 28087191,
        "value": null
      },
      {
        "id": 28087201,
        "value": null
      }
    ],
    "satisfaction_rating": null,
    "sharing_agreement_ids": [],
    "fields": [
      {
        "id": 28089621,
        "value": "jpa"
      },
      {
        "id": 28087191,
        "value": null
      },
      {
        "id": 28087201,
        "value": null
      }
    ],
    "brand_id": 782281
  }
]
next_page:
previous_page:
count: 2

但是现在我不知道如何在票证对象中加载接收到的数据。

我需要这样的东西:

foreach(Ticket t in [Received Data])
{
    ... do something
}

替换 "Console.WriteLine("{0}: {1}", 对。钥匙,对。值(;"部分。

这些是我在 JSON 中的第一步,一些操作方法或第一步链接也可能有所帮助。

(试图获得有关 http://www.newtonsoft.com/json 的提示,但没有成功(

更新 1:

在 dbc 的评论之后(谢谢,对于非常有用的链接(,我测试了更多一点。变量"内容":

var content = response.Content;

包含一个有效的 JSON 字符串:

{
    "tickets": [
        {
            "url": "https://xxx.zendesk.com/api/v2/tickets/2.json",
            "id": 2,
            "external_id": null,
            "via": {
                "channel": "web",
                "source": {
                    "from": {},
                    "to": {},
                    "rel": null
                }
            },
            "created_at": "2015-10-12T20:26:13Z",
            "updated_at": "2015-10-12T20:26:13Z",
            "type": "question",
            "subject": "test",
            "raw_subject": "test",
            "description": "test 1234",
            "priority": "normal",
            "status": "open",
            "recipient": null,
            "requester_id": 1371651941,
            "submitter_id": 1371651941,
            "assignee_id": 1371651941,
            "organization_id": 157600651,
            "group_id": 24976891,
            "collaborator_ids": [],
            "forum_topic_id": null,
            "problem_id": null,
            "has_incidents": false,
            "due_at": null,
            "tags": [
                "jpa"
            ],
            "custom_fields": [
                {
                    "id": 28089621,
                    "value": "jpa"
                },
                {
                    "id": 28087191,
                    "value": null
                },
                {
                    "id": 28087201,
                    "value": null
                }
            ],
            "satisfaction_rating": null,
            "sharing_agreement_ids": [],
            "fields": [
                {
                    "id": 28089621,
                    "value": "jpa"
                },
                {
                    "id": 28087191,
                    "value": null
                },
                {
                    "id": 28087201,
                    "value": null
                }
            ],
            "brand_id": 782281
        },
        {
            "url": "https://xxx.zendesk.com/api/v2/tickets/3.json",
            "id": 3,
            "external_id": null,
            "via": {
                "channel": "web",
                "source": {
                    "from": {},
                    "to": {},
                    "rel": null
                }
            },
            "created_at": "2015-10-13T12:22:39Z",
            "updated_at": "2015-10-13T12:22:39Z",
            "type": null,
            "subject": "Test 2",
            "raw_subject": "Test 2",
            "description": "test content",
            "priority": null,
            "status": "open",
            "recipient": null,
            "requester_id": 1371651941,
            "submitter_id": 1371651941,
            "assignee_id": 1371651941,
            "organization_id": 157600651,
            "group_id": 24976891,
            "collaborator_ids": [],
            "forum_topic_id": null,
            "problem_id": null,
            "has_incidents": false,
            "due_at": null,
            "tags": [],
            "custom_fields": [
                {
                    "id": 28089621,
                    "value": ""
                },
                {
                    "id": 28087191,
                    "value": null
                },
                {
                    "id": 28087201,
                    "value": null
                }
            ],
            "satisfaction_rating": null,
            "sharing_agreement_ids": [],
            "fields": [
                {
                    "id": 28089621,
                    "value": ""
                },
                {
                    "id": 28087191,
                    "value": null
                },
                {
                    "id": 28087201,
                    "value": null
                }
            ],
            "brand_id": 782281
        }
    ],
    "next_page": null,
    "previous_page": null,
    "count": 2
}

但是,仅当描述中没有双引号时(我认为在其他领域也是如此(。似乎 Zendesk API 不会转义引号?!

但无论如何我现在有有效的 JSON,如何在票证对象中加载 JSON 字符串?

将 Zendesk 工单获取到 SQL(JSON 处理)中

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://92logics.zendesk.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Bearer your_api_key"); // or your basic authentication
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string path = "api/v2/tickets.json?page=1";
ZenDeskTickets list = null;
                HttpResponseMessage response = await client.GetAsync(path);
                if (response.IsSuccessStatusCode)
                {
                    long SkippedId = 1;
                    string SkippedUrl = "";
                    list = await response.Content.ReadAsAsync<ZenDeskTickets>();
        }

您需要添加namespace System.Net.Http才能访问ReadAsAsync。它以 dll 形式提供,也可以作为 Web api 包文件提供

这是ZenDeskTickets给你的电话。

public class ZenDeskTickets
{
    [JsonProperty("tickets")]
    public Ticket[] Tickets { get; set; }
    [JsonProperty("next_page")]
    public object NextPage { get; set; }
    [JsonProperty("previous_page")]
    public object PreviousPage { get; set; }
    [JsonProperty("count")]
    public int Count { get; set; }
}

与定义票证类的方式相同。

每个属性/列将自动映射到正确的位置,并填充数据。