Microsoft.OData.ODataException:无效的JSON Whle正在CRM中创建事件



在CRM中创建Incident(case(时,我得到无效的JSON。无法创建Case验证输入参数时出错: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

标题:

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

我将"替换为";在上面的代码中。

最新更新