【问题标题】:Unable to parse JSON from Telnyx Api无法从 Telnyx Api 解析 JSON
【发布时间】: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


【解决方案1】:

首先,班级不好。应该是:

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 myDeserializedClass
{
    public Data data { get; set; }
    public Meta meta { get; set; }
}

这取决于您获取的数据,但如果您获取的是对象,则无需将其转换为工作:

[HttpPost]
public IActionResult InboundFax(myDeserializedClass json)
{
    try
    {
        //Work directly with json as object, forget "root" is: myDeserializedClass
        return Content("OK");
    }
    catch(Exception ex)
    {
        return Content(ex.ToString());
    }
}

或者如果您将 json 作为字符串获取:

[HttpPost]
public IActionResult InboundFax(string json)
{
    try
    {
        //Work directly with json as object
 myDeserializedClass myInstance= JsonConvert.DeserializeObject<myDeserializedClass>(json);
  
        return Content("OK");
    }
    catch(Exception ex)
    {
        return Content(ex.ToString());
    }
}

测试后更新:

我用一个虚拟控制器测试它:

 [HttpPost]
        public ActionResult InboundFax(myDeserializedClass json)
        {
            try
            {
                //Just dummy test
                if (json.meta.attempt == 1)
                {
                    return Content("OK");
                }
                else {
                    return Content("NO");
                }
                //Work directly with json as object, forget "root" is: myDeserializedClass
              
            }
            catch (Exception ex)
            {
                return Content(ex.ToString());
            }
        }

在 HomeController 中(来自 MVC Web 的空白模板只是虚拟的)

所以发帖到:

https://localhost:44334/Home/InboundFax

方法:POST

有以下数据:

{
  "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"
  }
}

你看到它映射一切的小快表:

是否意味着 POSTMAN 配置错误?

我使用以下标题:

Content-Type: application/json

我正在使用 TALEND API TESTER for Chrome,但每个 REST 客户端都相似

使用 POSTMAN,结果相同,OK。检查正文:原始,类型为 JSON,以及内容类型为申请/json 的标头

【讨论】:

  • 首先,非常感谢您的帮助。我创建了 DeserializedClass 类并复制了您发布的类。然后:``` public IActionResult InboundFax(DeserializedClass json) { try { return Content(json.data.payload.ToString()); } catch(Exception ex) { return Content(ex.ToString()); } }``` 但是当使用 Postman 发帖时,收到未设置为引用的对象错误。你能做到吗?
  • @Greybeard 没问题 不,我不能不付费使用 API :S
  • 对不起,我可能没听懂你的意思。您是否像 API 一样发布到您自己的服务?我可以看到带有标题和所有内容的整个示例吗?
  • 正确。就像 api 一样,我正在发布到我自己的服务。数据与该线程前面发布的数据相同。我也尝试使用 api 并收到相同的错误。所以我想知道您是否尝试使用 Postman 简单地发布数据并开始工作。我使用的所有代码和数据都在这篇文章中。
  • 您能尝试将数据发布到我的控制器吗?另外,我给你发了一封电子邮件。
【解决方案2】:

好吧,我不确定我是否有答案,但是,我确实设法通过将端点更改为 WebApi 而不是 MVC 控制器来使应用程序正常工作。我的印象是 MVC 控制器可以接受 json 数据,但是,我无法让它工作。一旦我改变了它,一切都完美无缺。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2011-05-23
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多