Microsoft.OData.ODataException:在 CRM 中创建事件时无效 JSON

Microsoft.OData.ODataException: Invalid JSON Whle Creating Incident in CRM

在 CRM 中创建事件(案例)时,我收到无效 JSON。无法创建案例 验证输入参数时出错:Microsoft.OData.ODataException:无效 JSON。在 JSON 内容的根目录中发现了多个值。

我错过了什么?

JSON

    {
"title": "SampleCase1CreatedByConsole",
"description": "This case is created by sample console app",
"customerid_account@odata.bind": "/accounts(000000000-0000-000-00000-0000000)"
 }

API URL:

https://xyzcrm.crm11.dynamics.com/api/data/v9.1/incidents

Headers:

Authorization:Bearer 
OData-MaxVersion:4.0
OData-Version:4.0
Accept:application/json

代码:

          public static async Task<string> CreateCase(string caseTitle, string customerId, string 
      caseDescription = "")
    {
        string jResu = "";
        string createcaseURL = "https://hack90182.crm11.dynamics.com/api/data/v9.1/incidents";
        try
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            string accessToken = "<ACCESSTOKEN>";

            //Body
            string jsonBody = "";
            string accountdatabind = string.Format("/accounts({0})", customerId);
            if (String.IsNullOrEmpty(caseDescription))
            {
                jsonBody = "{'title':'" + caseTitle + 
           "','customerid_account@odata.bind':'/accounts(00000-000-000-000000)'}}";
                //jsonBody = "{'title':'" + caseTitle +  "','customerid_account#odata.bind':'https://hack90182.crm11.dynamics.com/api/data/v9.1/accounts(00000-000-000-000000)'}}";
            }
            else
            {
                jsonBody = "{'title':'" + caseTitle + "','customerid_account@odata.bind':'" + 
          accountdatabind + "','description':'" + caseDescription + "'}}";
            }
            var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
            var client = new HttpClient();
            var message = new HttpRequestMessage(HttpMethod.Post, createcaseURL);
            message.Content = content;
            message.Headers.Add("OData-MaxVersion", "4.0");
            message.Headers.Add("OData-Version", "4.0");
            message.Headers.Add("Accept", "application/json");
            message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var result = client.SendAsync(message).Result;
            if (result != null)
            {
                var createcaseJSON = await result.Content.ReadAsStringAsync();
                jResu = createcaseJSON;//success
            }
            return jResu;

        }
        catch (Exception ex)
        {

            throw;
        }
    }

JSON 格式需要双引号而不是单引号,因此您必须以相同的方式设置 JSON 的格式。例如,

     jsonBody = "{\"title\":\"" + caseTitle + 
           "\",\"customerid_account@odata.bind\":\"/accounts(00000-000-000-000000)\"}";

我在上面的代码中用 \" 替换了 '。