【发布时间】: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
。 -
@Prany 在我粘贴的大JSON中有一个函数指针记录:里面有一个属性记录:我需要提取这个值并将其填充到一个c#对象中
标签: c# asp.net-core salesforce asp.net-web-api2