从 .NET Core Web API 调用 SalesForce API 并将响应反序列化为 c# 对象?
Posted
技术标签:
【中文标题】从 .NET Core Web API 调用 SalesForce API 并将响应反序列化为 c# 对象?【英文标题】:Call a SalesForce API from .NET Core Web API and deserialize the response in to a c# object? 【发布时间】:2018-12-15 04:10:51 【问题描述】:我是销售人员的新手。我正在尝试从我的 .Net Core Web API 调用我的销售人员 API。
使用下面的代码:
string new_Contact_url = "some URL";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new_Contact_url);
request.Method = "GET";
request.Headers.Add("Authorization", "Bearer 00D0E00000019dU!AQUAQK7Mcfutr7Y03dsuuKy9wyMt.GzRCUve6Rll8tDr2APzuHE.J2UjXXYt1vNPiQB5Fn2BCvoC7PwEwN9OLuyTbvTAp8Dj");
request.ContentType = "application/json; charset=utf-8";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
var resp = reader.ReadToEnd();
var obj = JsonConvert.DeserializeObject(resp);
//inplace of object I want to convert this into List<AccountDetail>
//List<AccountDetail>
通过在命令提示符中使用 Curl 命令,我得到这个响应为 json
"eTag" : "355105a",
"info" :
"cloneable" : true,
"createable" : true,
"deletable" : true,
"displayColumns" : [
"fieldApiName" : "Name",
"label" : "Account Name",
"sortable" : true
,
"fieldApiName" : "Parent.Name",
"label" : "Parent Account",
"sortable" : true
,
"fieldApiName" : "Type",
"label" : "Type",
"sortable" : true
,
"fieldApiName" : "Owner.FirstName",
"label" : "Owner First Name",
"sortable" : true
,
"fieldApiName" : "Owner.LastName",
"label" : "Owner Last Name",
"sortable" : true
],
"eTag" : "7e8d2",
"filterLogicString" : null,
"filteredByInfo" : [
"fieldApiName" : "CreatedDate",
"label" : "Created Date",
"operandLabels" : [ "6-6-2018" ],
"operator" : "GreaterThan"
],
"label" : "All Accounts",
"listReference" :
"id" : "00B0N000004w1WaUAI",
"listViewApiName" : "AllAccounts",
"objectApiName" : "Account",
"type" : "listView"
,
"orderedByInfo" : [
"fieldApiName" : "Name",
"isAscending" : true,
"label" : "Account Name"
],
"updateable" : true,
"userPreferences" :
"columnWidths" :
"Type" : -1,
"Parent.Name" : -1,
"Owner.FirstName" : -1,
"Owner.LastName" : -1,
"Name" : -1
,
"columnWrap" :
"Type" : false,
"Parent.Name" : false,
"Owner.FirstName" : false,
"Owner.LastName" : false,
"Name" : false
,
"visibility" : "Public",
"visibilityEditable" : true
,
"records" :
"count" : 2,
"currentPageToken" : "0",
"currentPageUrl" : "/Account/AllAccounts?pageSize=2&pageToken=0",
"listInfoETag" : "7e6aa798",
"nextPageToken" : "2",
"nextPageUrl" : "/Account/AllAccounts?pageSize=2&pageToken=2",
"previousPageToken" : null,
"previousPageUrl" : null,
"records" : [
"apiName" : "Account",
"childRelationships" : ,
"eTag" : "e6aa7986b72",
"fields" :
"CreatedDate" :
"displayValue" : null,
"value" : "2018-06-08T10:49:31.000Z"
,
"Id" :
"displayValue" : null,
"value" : "0010E00000M2f7aQAB"
,
"LastModifiedDate" :
"displayValue" : null,
"value" : "2018-06-25T09:05:08.000Z"
,
"Name" :
"displayValue" : null,
"value" : "-- UNKNOWN --"
,
"Owner" :
"displayValue" : null,
"value" :
"apiName" : "User",
"childRelationships" : ,
"eTag" : "4686ddfd6281",
"fields" :
"FirstName" :
"displayValue" : null,
"value" : "System"
,
"Id" :
"displayValue" : null,
"value" : "0050N000007jHIuQAM"
,
"LastName" :
"displayValue" : null,
"value" : "API User"
,
"id" : "0050N000007jHIuQAM",
"recordTypeInfo" : null
,
"OwnerId" :
"displayValue" : null,
"value" : "0050N000007jHIuQAM"
,
"Parent" :
"displayValue" : null,
"value" : null
,
"ParentId" :
"displayValue" : null,
"value" : null
,
"SystemModstamp" :
"displayValue" : null,
"value" : "2018-06-25T09:05:08.000Z"
,
"Type" :
"displayValue" : null,
"value" : null
,
"id" : "0010E00000M2f7aQAB",
"recordTypeInfo" : null
,
"apiName" : "Account",
"childRelationships" : ,
"eTag" : "815b8ac88",
"fields" :
"CreatedDate" :
"displayValue" : null,
"value" : "2018-06-08T10:48:00.000Z"
,
"Id" :
"displayValue" : null,
"value" : "0010"
,
"LastModifiedDate" :
"displayValue" : null,
"value" : "2018-06-25T09:04:33.000Z"
,
"Name" :
"displayValue" : null,
"value" : "AAA Transportation"
,
"Owner" :
"displayValue" : null,
"value" :
"apiName" : "User",
"childRelationships" : ,
"eTag" : "4686ddfd62818d54f92a08bd73726606",
"fields" :
"FirstName" :
"displayValue" : null,
"value" : "System"
,
"Id" :
"displayValue" : null,
"value" : "0050N000007jHIuQAM"
,
"LastName" :
"displayValue" : null,
"value" : "API User"
,
"id" : "0050N000007jHIuQAM",
"recordTypeInfo" : null
,
"OwnerId" :
"displayValue" : null,
"value" : "0050N000007jHIuQAM"
,
"Parent" :
"displayValue" : null,
"value" : null
,
"ParentId" :
"displayValue" : null,
"value" : null
,
"SystemModstamp" :
"displayValue" : null,
"value" : "2018-06-25T09:04:33.000Z"
,
"Type" :
"displayValue" : null,
"value" : null
,
"id" : "0010E000",
"recordTypeInfo" : null
]
我从这个回复中了解到,这里面有很多元数据信息。 我希望从响应中得到的重要部分是记录标签
我从记录标签创建了等效 C# 类
public class AccountDetail
public string Etag get; set;
public string CreatedDate get; set;
public string Id get; set;
public string LastModifiedDate get; set;
public string Name get; set;
public string ApiName get; set;
public List<OwnerDetails> Owners get; set;
public class OwnerDetails
public string Id get; set;
public string FirstName get; set;
public string LastName get; set;
public string ApiName get; set;
那么我如何从记录标签中提取信息到等效的 C# 对象列表中。这一行 var obj = (List)JsonConvert.DeserializeObject(resp);
【问题讨论】:
您需要确保正确匹配类型。例如:Owners
是请求正文中的一个对象,但您将其作为类型 List<T>
。另外,您是否考虑过在属性上使用 [JsonProperty()]
属性来帮助将它们正确映射到您的类?尝试使用这个website to generate your c# classes from json
你需要什么记录标签? (粗体)。抱歉,我没听懂
@MarkC。您能否为此发布一个示例,我以前没有使用过 JSONProperty。另外,我的理解是 Owners 是一个嵌套对象,所以我为它创建了一个单独的类,并在 Account details 中将其引用为 List你可以使用 Newtonsoft.Json
var files = JObject.Parse(YourJSON);
var recList = files.SelectTokens("$..records..records").ToList();
foreach (JObject obj in recList.Children())
foreach (JProperty prop in obj.Children())
var key = prop.Name.ToString();
var value = prop.Value.ToString();
//Do your stuffs here
【讨论】:
感谢 Prany,这正是我提取值所需要的。【参考方案2】:JsonConverter可以帮助你实现你所需要的。只需使用填充对象属性的所有必需规则为您的类实现转换器,并将其用于反序列化,例如:
List<AccountDetail> accountDetails = JsonConvert.DeserializeObject<List<AccountDetail>>(json, new ListOfAccountDetailsJsonConverter(typeof(List<AccountDetail>)));
【讨论】:
以上是关于从 .NET Core Web API 调用 SalesForce API 并将响应反序列化为 c# 对象?的主要内容,如果未能解决你的问题,请参考以下文章
CORS 不适用于从 Ajax 到 ASP.NET Core Web API 的调用
从 ASP.NET MVC 项目调用 .NET CORE 5 Web API 微服务时无法检索 BadRequest 错误
无法在 ASP.Net Core Web api 中启用 CORS
.NET Core - 在 IIS 上使用 Windows 身份验证从 MVC 应用程序调用 Web API 导致 HttpRequestException 401(未经授权)状态代码
本地 Javascript Fetch Post 请求失败,调用 ASP.NET Core 2.2 Web API。 CORS 已启用