【发布时间】:2021-11-22 16:44:47
【问题描述】:
我正在用 C# 开发一个端点,以接受从外部提供商 (Telnyx) 发布的 JSON。以下是数据示例:
{
"data": {
"event_type": "fax.received",
"id": "e15c28d4-147e-420b-a638-2a2647315577",
"occurred_at": "2021-11-19T16:37:02.863682Z",
"payload": {
"call_duration_secs": 35,
"connection_id": "1771912871052051547",
"direction": "inbound",
"fax_id": "2a168c93-3db5-424b-a408-b70a3da625bc",
"from": "+12399999999",
"media_url": "https://s3.amazonaws.com/faxes-prod/999",
"page_count": 1,
"partial_content": false,
"status": "received",
"to": "+12399999999",
"user_id": "dc6e79fa-fe3b-462b-b3a7-5fb7b3111b8a"
},
"record_type": "event"
},
"meta": {
"attempt": 1,
"delivered_to": "https://webhook.site/27ef892c-c371-4976-ae22-22deea57080e"
}
}
我已通过https://jsonlint.com/ 验证这是有效的 JSON。我创建了一个模型:
public class myDeserializedClass
{
public class Payload
{
public int call_duration_secs { get; set; }
public string connection_id { get; set; }
public string direction { get; set; }
public string fax_id { get; set; }
public string from { get; set; }
public string media_url { get; set; }
public int page_count { get; set; }
public bool? partial_content { get; set; }
public string status { get; set; }
public string to { get; set; }
public string user_id { get; set; }
}
public class Data
{
public string event_type { get; set; }
public string id { get; set; }
public DateTime occurred_at { get; set; }
public Payload payload { get; set; }
public string record_type { get; set; }
}
public class Meta
{
public int attempt { get; set; }
public string delivered_to { get; set; }
}
public class Root
{
public Data data { get; set; }
public Meta meta { get; set; }
}
}
发布到的控制器如下所示:
[HttpPost]
public IActionResult InboundFax(myDeserializedClass json)
{
try
{
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json.ToString().Trim());
return Content("OK");
}
catch(Exception ex)
{
return Content(ex.ToString());
}
}
每次 API 尝试发布到我的端点时,我都会收到错误消息:Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: K. Path '', line 0, position 0.。我也尝试过使用 Postman 发布数据并收到相同的错误消息。此外,API 网站https://developers.telnyx.com/docs/v2/programmable-fax/tutorials/receive-a-fax-via-api 上还有 JSON 发布示例。由于我的应用程序因邮递员和实时 API 调用而失败,我正在假设问题是我的代码,但不能 100% 确定并且不知道如何修复它。这是我需要解决的关键任务问题。任何帮助将不胜感激。
【问题讨论】:
-
您好!我现在知道了。让我们尝试简化事情。首先,当你调试时,json 是否很好地填充了输入 myDeserializedClass 上的数据?
-
我想我现在明白了问题所在。请查看我的更新,我是如何在另一个课程中没有课程的情况下格式化课程的。以及如何实现该类
标签: asp.net-core json.net