gpt4 book ai didi

c# - 获取存储在 hubspot 中的所有表单作为 Dictionary()

转载 作者:行者123 更新时间:2023-12-05 05:46:28 25 4
gpt4 key购买 nike

是否通过 Hubspot 表单 api 以某种方式获取特定格式的所有表单。

根据文档和预览,它似乎返回的是一个 json 对象列表。

https://legacydocs.hubspot.com/docs/methods/forms/v2/get_forms

是否可以通过 api 以某种方式更改此格式?

我很可能将它作为字典返回,其中每个条目的键是 guid,值是表单本身。

我认为它可能的唯一方法是对接收到的输入进行后处理,然后将其映射到字典中,但如果可以通过 api 实现,这似乎有点多余?

最佳答案

使用 NewtonSoft 的 JsonConvert 和 JsonPropertyAttributes 反序列化 JSON:

使用 JsonPropertyAttribute 使用您需要的属性创建类...像这样:

public class FormObj
{
[JsonProperty(PropertyName = "portalId")]
public int PortalId { get; set; }

[JsonProperty(PropertyName = "guid")]
public Guid Guid { get; set; }

[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

[JsonProperty(PropertyName = "action")]
public string Action { get; set; }

[JsonProperty(PropertyName = "method")]
public string Method { get; set; }

[JsonProperty(PropertyName = "cssClass")]
public string CssClass { get; set; }

[JsonProperty(PropertyName = "redirect")]
public string Redirect { get; set; }

[JsonProperty(PropertyName = "submitText")]
public string SubmitText { get; set; }

[JsonProperty(PropertyName = "followUpId")]
public string FollowUpId { get; set; }

[JsonProperty(PropertyName = "notifyRecipients")]
public string NotifyRecipients { get; set; }

[JsonProperty(PropertyName = "leadNurturingCampaignId")]
public string LeadNurturingCampaignId { get; set; }

[JsonProperty(PropertyName = "createdAt")]
public long CreatedAt { get; set; }

[JsonProperty(PropertyName = "updatedAt")]
public long UpdatedAt { get; set; }

[JsonProperty(PropertyName = "performableHtml")]
public string PerformableHtml { get; set; }

[JsonProperty(PropertyName = "migratedFrom")]
public string MigratedFrom { get; set; }

[JsonProperty(PropertyName = "ignoreCurrentValues")]
public bool IgnoreCurrentValues { get; set; }

[JsonProperty(PropertyName = "formFieldGroups")]
public FormFieldGroup[] FormFieldGroups { get; set; }

[JsonProperty(PropertyName = "metaData")]
public MetaData[] MetaData { get; set; }

[JsonProperty(PropertyName = "deletable")]
public bool IsDeletable { get; set; }

[JsonProperty(PropertyName = "inlineMessage")]
public string InlineMessage { get; set; }

[JsonProperty(PropertyName = "tmsId")]
public string? tmsId { get; set; }

[JsonProperty(PropertyName = "captchaEnabled")]
public bool IsCaptchaEnabled { get; set; }

[JsonProperty(PropertyName = "campaignGuid")]
public string CampaignGuid { get; set; }

[JsonProperty(PropertyName = "cloneable")]
public bool IsCloneable { get; set; }

[JsonProperty(PropertyName = "editable")]
public bool IsEditable { get; set; }

[JsonProperty(PropertyName = "formType")]
public string FormType { get; set; }
}

public class FormFieldGroup
{
[JsonProperty(PropertyName = "fields")]
public IEnumerable<Field> Fields { get; set; }

// ...
}

public class Field
{

[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

[JsonProperty(PropertyName = "label")]
public string DisplayName { get; set; }

// ...
}

public class MetaData
{
// ...
}

FormFieldGroup、Field 和 MetaData 对象并不完整,希望您可以从示例中推断出所需的属性。您可以根据需要命名您的属性,您只需要 JsonPropertyAttribute 的 PropertyName 来匹配 JSON 中的属性。您也不必拥有对象中 JSON 中的每个属性,您拥有完全的控制权。

反序列化为 FormObj 的 IEnumerable,然后将其转换为字典是相当直接的。

var formDictionary = JsonConvert.DeserializeObject<IEnumerable<FormObj>>(JSON).ToDictionary(v => v.Guid);

综上所述,创建您自己的客户端可能看起来像这样:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace YourNamespaceHere
{
public static class HubSpotClient
{
private static Uri baseUri = new Uri(@"https://api.hubapi.com");

public static async Task<IDictionary<Guid, FormObj>> GetForms()
{
var forms = await Get<IEnumerable<FormObj>>(@"/forms/v2/forms?hapikey=demo");
return forms.ToDictionary(f => f.Guid);
}

private static async Task<T> Get<T>(string relativePath)
{
var json = await CallHubspot(relativePath);
return JsonConvert.DeserializeObject<T>(json);
}

private static async Task<string> CallHubspot(string relativePath)
{
using (var client = new HttpClient())
{
client.BaseAddress = baseUri;
try
{
HttpResponseMessage response = await client.GetAsync(relativePath);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (HttpRequestException e)
{
// add logging or better error handling
throw;
}
}
}
}
}

关于c# - 获取存储在 hubspot 中的所有表单作为 Dictionary<Guid, object>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71175369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com