英文:
Using JsonContent.Create for properties that are not valid C#
问题
I like JsonContent.Create
. It allows me to create a POST body like this:
使用 JsonContent.Create
我可以创建一个类似这样的 POST 主体:
using var message = new HttpRequestMessage(HttpMethod.Post, myUrl);
message.Content = JsonContent.Create(new { displayName = myDisplayName });
This is great for properties like displayName
. But now I am trying to call an MS Graph service, and it requires me to put this in as a property:
这对于像 displayName
这样的属性非常好。但现在我正在尝试调用 MS Graph 服务,它要求我将这个值作为一个属性放入:
@odata.id = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484"
@odata.id
is not a valid C# identifier.
@odata.id
不是一个有效的 C# 标识符。
Is there a way to use JsonContent.Create
with odd properties like @odata.id
?
有没有办法使用 JsonContent.Create
处理像 @odata.id
这样的特殊属性?
英文:
I like JsonContent.Create
. It allows me to create a POST body like this:
using var message = new HttpRequestMessage(HttpMethod.Post, myUrl);
message.Content = JsonContent.Create(new { displayName = myDisplayName});
This is great for properties like displayName
. But now I am trying to call an MS Graph service, and it requires me to put this in as a property:
@odata.id = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484"
@odata.id
is not a valid C# identifier.
Is there a way to use JsonContent.Create
with odd properties like @odata.id
?
答案1
得分: 3
你可以尝试使用字典:
var jsonContent = JsonContent.Create(new Dictionary<string, object>
{
{ "@odata.id", "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484" }
});
或者创建带有适当属性标记的自定义类:
public class MyClass1
{
[JsonPropertyName("@odata.id")]
public string OdataId { get; set; }
}
var jsonContent = JsonContent.Create(new MyClass1
{
OdataId = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484"
});
英文:
You can try using dictionaries:
var jsonContent = JsonContent.Create(new Dictionary<string, object>
{
{ "@odata.id", "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484" }
});
Or create custom class marking field with proper attribute:
public class MyClass1
{
[JsonPropertyName("@odata.id")]
public string OdataId { get; set; }
}
var jsonContent = JsonContent.Create(new MyClass1
{
OdataId = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484"
});
答案2
得分: 2
你可以使用以 string
为键类型的字典。将 JSON 对象视为 Dictionary<string, object>
的实例(或某些更专业化的值类型,而不仅仅是 object)。
var dict = new Dictionary<string, object>();
dict["@odata.id"] = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484";
message.Content = JsonContent.Create(dict);
英文:
You can use a dictionary with string
as key type. Think of json objects being instances of Dictionary<string, object>
(or some other appropriate type for the values that's more specialized than object).
var dict = new Dictionary<string, object>();
dict["@odata.id"] = "https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484";
message.Content = JsonContent.Create(dict);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论