如何从.NET中的ODATA SuccessFactors API中验证对象
我想在.NET项目中阅读一些SuccessFactor(SAP HR系统)数据。使用ODATA V2 API的成功量程序使数据可访问。我使用以下代码读取用户:
using (var client = new HttpClient())
{
var credential = Encoding.ASCII.GetBytes("username:pwd");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credential));
var json = await
client.GetStringAsync($"https://successfactorsServer.sapsf.eu/odata/v2/User?$filter=userId eq '{userId}'&$select=firstName, lastName");
var user = JsonConvert.DeserializeObject<User>(json);
return user;
}
问题是该服务提供了该代码
<id>https://api12preview.sapsf.eu/odata/v2/User('MaxMustermann')</id>
<title type="text"></title>
<updated>2022-06-17T20:27:31Z</updated>
<author>
<name></name>
</author>
<link rel="edit" title="User" href="User('MaxMustermann')"></link>
<category term="SFOData.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"></category>
<content type="application/xml">
<m:properties>
<d:firstName>Max</d:firstName>
<d:lastName>Mustermann</d:lastName>
</m:properties>
</content>
,但这无法转换为我的模型对象:public class用户:inotifyPropertychanged { public用户(){}
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => FirstName + " " + LastName;
public event PropertyChangedEventHandler PropertyChanged;
}
有一种方法可以解决此问题。因此,将一个名称和最后一个名称识别并转移到用户对象中? 我不想在此JSON结构中构建我的C#对象。而且我不确定如何处理D:在JSON属性名称前。
可能有更好的图书馆吗?
I want to read some SuccessFactors (SAP HR System) Data in my .net project. SuccessFactors using OData v2 API's to make data accessible. I use the following code to read a User:
using (var client = new HttpClient())
{
var credential = Encoding.ASCII.GetBytes("username:pwd");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credential));
var json = await
client.GetStringAsync(quot;https://successfactorsServer.sapsf.eu/odata/v2/User?$filter=userId eq '{userId}'&$select=firstName, lastName");
var user = JsonConvert.DeserializeObject<User>(json);
return user;
}
The problem is that the service delivers the
<id>https://api12preview.sapsf.eu/odata/v2/User('MaxMustermann')</id>
<title type="text"></title>
<updated>2022-06-17T20:27:31Z</updated>
<author>
<name></name>
</author>
<link rel="edit" title="User" href="User('MaxMustermann')"></link>
<category term="SFOData.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"></category>
<content type="application/xml">
<m:properties>
<d:firstName>Max</d:firstName>
<d:lastName>Mustermann</d:lastName>
</m:properties>
</content>
But this is not convertable to my model object:public class User : INotifyPropertyChanged
{
public User() { }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => FirstName + " " + LastName;
public event PropertyChangedEventHandler PropertyChanged;
}
Is there a way with newtonsoft JsonConverter to solve this. So that firstName and lastName are recognized and transfered to a User object?
I don't want to build my C# objects exactly in this JSON Structure. And i'm not sure how to handle the d: in front of JSON Property Names.
Is there a better library maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从API中获得XML而不是JSON。如果无法修复API,则必须将XML转换为C#对象
you are getting xml instead of json from Api. If you can't fix Api you will have to convert xml to c# object
如所描述的在这里
只需要JSON响应的客户端将其HTTP标头设置为“ Application/JSON”。
例如:
要添加标题信息,请使用
As described here:
A client that wants only JSON responses would set its http header to "application/json".
For example:
To add the header information, use