1

我正在调用销售人员 api 来获取搜索结果。为了连接到销售人员,我正在使用 force 客户端。

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

我为此进行了很多搜索,但无法弄清楚出了什么问题。

4

1 回答 1

0

问题是由反序列化响应的SearchAsync方法引起的。ForceClient

客户端尝试将来自服务器的响应反序列化为 aList<T>但是,正如我们在 JSON 中看到的那样,响应首先包装在一个searchRecords元素中,因此反序列化会引发您看到的异常。

要解决此问题,您需要执行以下操作:

更改SearchAsync调用以使用dynamic类型:

var response = await client.SearchAsync<dynamic>(query);

将新类型的属性反序列searchRecords化为一个数组SearchRecord

var records = JsonConvert.DeserializeObject<List<SearchRecord>>(list.searchRecords.ToString());

您现在可以使用该records变量来读取从 API 调用返回的数据。

注意 作为参考,我查看了GitHub 上的源代码,找到了SearchAsync方法的工作原理,然后查看了测试以了解它们是如何从SearchAsync调用中检索数据的。向我展示答案的单元测试代码在这里

于 2018-10-24T23:54:50.943 回答