Newtonsoft.Json.JsonReaderException: 解析值时遇到意外字符 - ASP.NET
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value - ASP.NET
如何在 ASP.NET Core Web API 中使用 Telegram.Bot 包开发 Telegram 机器人时正确反序列化 POST 方法“更新”?
我使用 DeserializeObject(string) 方法这样做,但它一直返回错误:Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value
代码:
using Microsoft.AspNetCore.Mvc;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using Newtonsoft.Json;
namespace OfficeBooking.TelegramApi.Controllers
{
[ApiController]
[Route(template: "api/message")]
public class TelegramBotController : ControllerBase
{
private readonly TelegramBotClient _telegramBotClient;
public TelegramBotController(TelegramBot telegramBot)
{
_telegramBotClient = telegramBot.GetBot().Result;
}
[HttpPost]
public async Task <IActionResult> Update([FromBody]object update)
{
var upd = JsonConvert.DeserializeObject<Update>(update.ToString());
var chat = upd.Message?.Chat;
if (chat == null)
{
return Ok();
}
await _telegramBotClient.SendTextMessageAsync(
chatId: chat.Id,
text: "Testing sendMessage method",
parseMode: ParseMode.MarkdownV2,
disableNotification: false,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
"Check sendMessage method",
"https://core.telegram.org/bots/api#sendmessage")));
return Ok();
}
}
}
我认为您应该尝试依靠 ASP.NET Core 来解析您的 json 输入。您可以简单地指定类型 Update 而不是 object ,它将为您完成剩下的工作。您只需要使用类型为 Update:
的现成对象
[HttpPost]
public async Task <IActionResult> Update([FromBody]Update update)
{
var chat = update.Message?.Chat;
if (chat == null)
{
return Ok();
}
await _telegramBotClient.SendTextMessageAsync(
chatId: chat.Id,
text: "Testing sendMessage method",
parseMode: ParseMode.MarkdownV2,
disableNotification: false,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
"Check sendMessage method",
"https://core.telegram.org/bots/api#sendmessage")));
return Ok();
}
如果您希望框架使用 NewtonsoftJson 而不是默认的 Json 实用程序(在您的情况下这不是必需的),请遵循此说明 https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/
如何在 ASP.NET Core Web API 中使用 Telegram.Bot 包开发 Telegram 机器人时正确反序列化 POST 方法“更新”? 我使用 DeserializeObject(string) 方法这样做,但它一直返回错误:Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value
代码:
using Microsoft.AspNetCore.Mvc;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using Newtonsoft.Json;
namespace OfficeBooking.TelegramApi.Controllers
{
[ApiController]
[Route(template: "api/message")]
public class TelegramBotController : ControllerBase
{
private readonly TelegramBotClient _telegramBotClient;
public TelegramBotController(TelegramBot telegramBot)
{
_telegramBotClient = telegramBot.GetBot().Result;
}
[HttpPost]
public async Task <IActionResult> Update([FromBody]object update)
{
var upd = JsonConvert.DeserializeObject<Update>(update.ToString());
var chat = upd.Message?.Chat;
if (chat == null)
{
return Ok();
}
await _telegramBotClient.SendTextMessageAsync(
chatId: chat.Id,
text: "Testing sendMessage method",
parseMode: ParseMode.MarkdownV2,
disableNotification: false,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
"Check sendMessage method",
"https://core.telegram.org/bots/api#sendmessage")));
return Ok();
}
}
}
我认为您应该尝试依靠 ASP.NET Core 来解析您的 json 输入。您可以简单地指定类型 Update 而不是 object ,它将为您完成剩下的工作。您只需要使用类型为 Update:
的现成对象 [HttpPost]
public async Task <IActionResult> Update([FromBody]Update update)
{
var chat = update.Message?.Chat;
if (chat == null)
{
return Ok();
}
await _telegramBotClient.SendTextMessageAsync(
chatId: chat.Id,
text: "Testing sendMessage method",
parseMode: ParseMode.MarkdownV2,
disableNotification: false,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
"Check sendMessage method",
"https://core.telegram.org/bots/api#sendmessage")));
return Ok();
}
如果您希望框架使用 NewtonsoftJson 而不是默认的 Json 实用程序(在您的情况下这不是必需的),请遵循此说明 https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/