使用JsonContent.Create处理不是有效的C#属性。

huangapple go评论77阅读模式
英文:

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&lt;string, object&gt;
{
	{ &quot;@odata.id&quot;, &quot;https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484&quot; }
});

Or create custom class marking field with proper attribute:

public class MyClass1
{
	[JsonPropertyName(&quot;@odata.id&quot;)]
	public string OdataId { get; set; }
}

var jsonContent = JsonContent.Create(new MyClass1
{
	OdataId = &quot;https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484&quot;
});

答案2

得分: 2

你可以使用以 string 为键类型的字典。将 JSON 对象视为 Dictionary&lt;string, object&gt; 的实例(或某些更专业化的值类型,而不仅仅是 object)。

var dict = new Dictionary&lt;string, object&gt;();
dict[&quot;@odata.id&quot;] = &quot;https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484&quot;;

message.Content = JsonContent.Create(dict);
英文:

You can use a dictionary with string as key type. Think of json objects being instances of Dictionary&lt;string, object&gt; (or some other appropriate type for the values that's more specialized than object).

var dict = new Dictionary&lt;string, object&gt;();
dict[&quot;@odata.id&quot;] = &quot;https://graph.microsoft.com/v1.0/directoryObjects/46026638-208d-4c03-bb85-d42f0c472484&quot;;


message.Content = JsonContent.Create(dict);

huangapple
  • 本文由 发表于 2023年8月4日 06:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定