0

我会尽量提供尽可能多的信息。我已经尝试了几个小时来解决这个问题,但我一直无法解决,这就是我在这里的原因。请帮我。

我正在尝试在 Hubspot 上创建一个新的公司属性。这是我正在使用的代码:

foreach (CompanyPropertyResponse prop in companyStagingdata)
            {
                var founditems = Testhubspotproperties.Find(i => i.name == prop.name);

                if (founditems == null)
                {
                    //runner.LogInformational($"We need property Group: {group.name}");
                    //Create propery group
                    var createcompanypropertyurl = "https://api.hubapi.com/properties/v1/companies/properties?hapikey=" + HubspotService.DestinationAPIKey;
                    var createpropertyforcompany = new CompanyPropertyResponse
                    {
                        name = prop.name,
                        label = prop.label,
                        description = prop.description,
                        groupName = prop.groupName,
                        type = prop.type,
                        fieldType = prop.fieldType
                    };

                    if (prop.options.Count > 0)
                    {
                        createpropertyforcompany.options = new List<Option>();
                        foreach (var option in prop.options)
                        {
                            createpropertyforcompany.options.Add(new Option
                            {
                               label = option.label,
                               value = option.value
                            });
                        }
                    }

                    if (prop.type == "enumeration" && prop.options.Count == 0)
                    {
                            createpropertyforcompany.type = "string";
                            createpropertyforcompany.fieldType = "text";
                    }

                    string JsonCheck = JsonConvert.SerializeObject(createpropertyforcompany).ToString();

                    if (!JsonHelper.IsValidJson(JsonCheck))
                    {
                        continue;
                    }

                    try
                    {
                        WebRequest postcompanyproperty_webRequest = Helper.GetWebRequest(createcompanypropertyurl, JsonCheck, "POST");
                        postcompanyproperty_webRequest.GetResponse();
                    }
                    catch (Exception e)
                    {
                        //runner.LogError(e, JsonConvert.SerializeObject(createpost));
                        throw;
                    }
                    //runner.LogInformational($"Company Group Created: {group.name}");
                }
            }

我正在比较一个已经存在的 Hubspot 帐户,并且我想将一个帐户与另一个帐户同步。

如果目标帐户中不存在属性,则从源帐户创建它。

如您所见,我试图通过以下方法解析 JSON 是否有效JsonHelper.IsvalidJson

public static bool IsValidJson(string strInput)
        {
            try
            {
                var obj = JObject.Parse(strInput);
                return true;
            }
            catch
            {
                return false;
            }
        }

但是,我仍然在这部分代码上遇到异常:

try
                    {
                        WebRequest postcompanyproperty_webRequest = Helper.GetWebRequest(createcompanypropertyurl, JsonCheck, "POST");
                        postcompanyproperty_webRequest.GetResponse();
                    }
                    catch (Exception e)
                    {
                        //runner.LogError(e, JsonConvert.SerializeObject(createpost));
                        throw;
                    }

我一直在查看 Hubspot 上显示的 API POST 和 GET 记录,这些是错误:源帐户:

{
  "status": "error",
  "message": "Invalid input JSON on line 1, column 42: Unexpected character (',' (code 44)): expected a value",
  "correlationId": "9934949d-e73a-4881-b83e-e791098b45ad"
}

目的地账户:

{
  "status": "error",
  "message": "Unable to process JSON",
  "correlationId": "7e07b8ad-ae51-4ac0-894c-88b0893fcc6d"
}

最后,这是当代码尝试发布新属性时传入的 JSON:

{
    "name": "activation_timestamp",
    "label": "Activation Timestamp",
    "description": "Date Fusebill logged account activation",
    "groupName": "account_details",
    "type": "date",
    "fieldType": "date",
    "fieldLevelPermission": null,
    "hidden": false,
    "updatedAt": null,
    "referencedObjectType": null,
    "optionSortStrategy": null,
    "createdUserId": null,
    "externalOptionsReferenceType": null,
    "searchableInGlobalSearch": false,
    "hasUniqueValue": false,
    "numberDisplayHint": null,
    "readOnlyDefinition": false,
    "externalOptions": false,
    "textDisplayHint": null,
    "displayOrder": 0,
    "isCustomizedDefault": false,
    "formField": false,
    "readOnlyValue": false,
    "mutableDefinitionNotDeletable": false,
    "favorited": false,
    "favoritedOrder": 0,
    "calculated": false,
    "displayMode": null,
    "showCurrencySymbol": null,
    "optionsAreMutable": null,
    "searchTextAnalysisMode": null,
    "createdAt": null,
    "hubspotDefined": null,
    "currencyPropertyName": null,
    "updatedUserId": null,
    "deleted": null,
    "options": null
}
4

1 回答 1

0

我找到了解决方案。我的 JSON 值应该null作为字符串传入,"null"而 Hubspot 不喜欢这样。

我在序列化我的 Json 时使用了以下内容,以便它忽略所有null值。

var json = JsonConvert.SerializeObject(
        objectToSerialize,
        new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
于 2022-02-21T20:58:29.640 回答