我正在致电Salesforce API以获取搜索结果。为了连接到销售人员,我使用了强制客户端。
var client = GetForceClient(token);
string query = "FIND {01} IN ALL FIELDS RETURNING CASE(Id,Account.Name,Case_Id__c,Customer_Ticket_Id__c,Site__r.Name,Order__r.Name,Site__r.Country__c,Status,Subject LIMIT 5 OFFSET 0),Order__c(Order_Name__c,Tech_Service_Type__C,Customer__r.Name,End_Customer__r.Name,Site__r.Site_Name__c,name LIMIT 5 OFFSET 0),SITE__C(ID,Status__c,Address__c,Site_Name__c,NAME)";
var response = await client.SearchAsync<Search>(query);
在此行中反序列化结果时
var response = await client.SearchAsync<Search>(query);
正在引发以下错误。
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[API.Dto.Search]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'searchRecords', line 1, position 17.
json响应看起来像这样
{
"searchRecords" : [ {
"attributes" : {
"type" : "Case",
"url" : "/services/data/v43.0/sobjects/Case/5000D"
},
"Id" : "5000D",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v43.0/sobjects/Account/0010"
},
"Name" : "AAPTxxvvbb"
},
"Case_Id__c" : "CLIC-0001234",
"Customer_Ticket_Id__c" : "33FWQ23",
"Site__r" : null,
"Order__r" : {
"attributes" : {
"type" : "Order__c",
"url" : "/services/data/v43.0/sobjects/Order__c/a060D"
},
"Name" : "ORD-602"
},
"Status" : "Open",
"Subject" : "Please Provide Your Reference"
}, {
"attributes" : {
"type" : "Order__c",
"url" : "/services/data/v43.0/sobjects/Order__c/a060D"
},
"Order_Name__c" : "Bro 2K",
"Tech_Service_Type__c" : "Bro",
"Customer__r" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v43.0/sobjects/Account/0010"
},
"Name" : "XT"
},
"End_Customer__r" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v43.0/sobjects/Account/0010"
},
"Name" : "Ran"
},
"Site__r" : {
"attributes" : {
"type" : "Site__c",
"url" : "/services/data/v43.0/sobjects/Site__c/a0A0"
},
"Site_Name__c" : "Ran01"
},
"Name" : "ORD-6025"
}, {
"attributes" : {
"type" : "Site__c",
"url" : "/services/data/v43.0/sobjects/Site__c/a0A0"
},
"Id" : "a0A0",
"Status__c" : "In Progress",
"Address__c" : "Rue",
"Site_Name__c" : "Ran01",
"Name" : "SIT-0154"
} ]
}
等效的C#类是这样的:
public class Search
{
[JsonProperty("searchRecords")]
public SearchRecord[] SearchRecords { get; set; }
}
public class SearchRecord
{
[JsonProperty("attributes")]
public Attributes Attributes { get; set; }
[JsonProperty("Id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; set; }
[JsonProperty("Account", NullValueHandling = NullValueHandling.Ignore)]
public Account Account { get; set; }
[JsonProperty("Case_Id__c", NullValueHandling = NullValueHandling.Ignore)]
public string CaseIdC { get; set; }
[JsonProperty("Customer_Ticket_Id__c", NullValueHandling = NullValueHandling.Ignore)]
public string CustomerTicketIdC { get; set; }
[JsonProperty("Site__r", NullValueHandling = NullValueHandling.Ignore)]
public SiteR SiteR { get; set; }
[JsonProperty("Order__r", NullValueHandling = NullValueHandling.Ignore)]
public Account OrderR { get; set; }
[JsonProperty("Status", NullValueHandling = NullValueHandling.Ignore)]
public string Status { get; set; }
[JsonProperty("Subject", NullValueHandling = NullValueHandling.Ignore)]
public string Subject { get; set; }
[JsonProperty("Order_Name__c", NullValueHandling = NullValueHandling.Ignore)]
public string OrderNameC { get; set; }
[JsonProperty("Tech_Service_Type__c", NullValueHandling = NullValueHandling.Ignore)]
public string TechServiceTypeC { get; set; }
[JsonProperty("Customer__r", NullValueHandling = NullValueHandling.Ignore)]
public Account CustomerR { get; set; }
[JsonProperty("End_Customer__r", NullValueHandling = NullValueHandling.Ignore)]
public Account EndCustomerR { get; set; }
[JsonProperty("Name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
[JsonProperty("Status__c")]
public object StatusC { get; set; }
[JsonProperty("Address__c")]
public string AddressC { get; set; }
[JsonProperty("Site_Name__c", NullValueHandling = NullValueHandling.Ignore)]
public string SiteNameC { get; set; }
}
public partial class Attributes
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
}
我为此进行了很多搜索,但无法找出问题所在。
答案 0 :(得分:0)
问题是由SearchAsync
的{{1}}方法如何反序列化响应引起的。
客户端尝试将来自服务器的响应反序列化为ForceClient
,但是,正如我们在JSON中看到的那样,响应首先包装在List<T>
元素中,因此反序列化会引发异常看到。
要解决此问题,您需要执行以下操作:
将searchRecords
调用更改为使用SearchAsync
类型:
dynamic
将新类型的var response = await client.SearchAsync<dynamic>(query);
属性反序列化为searchRecords
的数组
SearchRecord
您现在可以使用var records = JsonConvert.DeserializeObject<List<SearchRecord>>(list.searchRecords.ToString());
变量来读取从API调用返回的数据。
注意
作为参考,我查看了source on GitHub,找到了SearchAsync方法的工作方式,然后查看了测试以了解它们如何从records
调用中检索数据。向我显示答案的单元测试代码为here