【发布时间】:2020-12-15 03:34:13
【问题描述】:
json
{
"id": 261111,
"date": "2020-08-26 15:06:04",
"ts": 1598447164,
"message-id": "<5f462cb575dfda4548a880d4@domain.com>",
"ts_event": 1598447164
}
我的模型类是
class Model{
public string id { get; set; }
public string date { get; set; }
public string message_id { get; set; }
public string ts { get; set; }
public string ts_event { get; set; }
}
我的 webMethod 如下
public response Post(Model model)
{
string message_Id= model.message_id;
}
在执行此操作时,我得到了 model.message_id 的空值,因此我将代码重构为
class Model{
public string id { get; set; }
public string date { get; set; }
[JsonProperty("message-id")] //I have use newtonsoft.json
public string message_id { get; set; }
public string ts { get; set; }
public string ts_event { get; set; }
}
这也不起作用我的目标是在 web 方法中获取 json 的值(消息 ID)。并存储在后端
【问题讨论】:
-
这确实对我有用
[JsonProperty("message-id")] -
[JsonProperty("message-id")]与JsonConvert.DeserializeObject一起为我工作。 -
dotnetfiddle.net/KfDIWl 工作代码在这里。
-
@SowmyadharGourishetty 我正在尝试实现 webhook。我看到了您使用 DeserializeObject 的示例代码,但我想将 json 值自动绑定到我的模型类,我已将其作为参数传递给我的 webMethod。感谢大家的快速响应和指导。