将Zendesk门票提取到SQL(JSON处理)

时间:2015-10-12 21:34:39

标签: c# json

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

我在C#ConsoleApp中使用了文章中的代码进行测试:

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

但现在我不知道如何在Ticket Objects中加载收到的数据。

我需要这样的东西:

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

替换" Console.WriteLine(" {0}:{1}",pair.Key,pair.Value);"一部分。

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

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

更新1:

来自dbc的评论(thx,对于非常有用的链接),我再试一点。变量"内容":

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,如何在Ticket对象中加载JSON字符串?

1 个答案:

答案 0 :(得分:0)

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; }
}

您可以定义Ticket Class。

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