【问题标题】:Setting property name to include @ in JSON using System.Text.Json使用 System.Text.Json 设置属性名称以在 JSON 中包含 @
【发布时间】:2021-08-31 13:03:50
【问题描述】:

我需要通过 REST 发送(不包括其他一些敏感信息):

{"@Scope": "Local"}

类文件中我JsonProperty为Json.Net序列化:

[JsonProperty("@Scope")]
public string Scope { get; set; }

将要通过 WebRequest 发送的 json 消息放在一起时,我有

{@Scope = "Local"}

但运行后:

string jsonString = System.Text.Json.JsonSerializer.Serialize(message);

发出的消息是

{"Scope": "Local"}

出于某种原因,我认为 System.Text.Json 会获取 JsonProperty 属性,但显然应该有其他方式。

【问题讨论】:

标签: c# json system.text.json


【解决方案1】:

尝试使用

[JsonPropertyName("@Scope")]

而不是

[JsonProperty("@Scope")]

JsonProperty 可能用于 Newtonsoft,内置序列化库可能无法识别。

【讨论】:

  • 感谢您的快速回复。这似乎有效。
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2012-03-13
相关资源
最近更新 更多