【发布时间】:2019-04-18 07:43:17
【问题描述】:
我正在使用以下代码向 WebAPI 发送请求:
client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form)))
其中client 是HttpClient 类的对象。对 WebApi 的所有请求执行此代码。我正在尝试将以下数据发送到 API:
{
"code":"GUEST",
"category":"Indian",
"sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],
"date":"0001-01-01T00:00:00",
"time":"0001-01-01T00:00:00",
"quantity":1.0,
"price":0.0,
"discount":0.0,
"paymentMethod":"ID",
"paymentMethodID":null,
"ticketNo":null
}
现在因为 FormUrlEncodedContent 只接受 Dictionary<string,string> 对象,我正在使用 NewtonSoft 的 JSON.NET 方法 JsonConvert.DeserializeObject 将此 JSON 转换为该类型。但是在 sections 数组开始的地方,它向我显示了以下错误消息:
Unexpected character encountered while parsing value:[. Path 'sections'.
那么,如果我想对这种 JSON 数据使用相同的代码,我应该遵循什么方法?
【问题讨论】:
-
您将无法将其作为 FormUrlEncodedContent 发送。您不能简单地将 json 作为您的 post 请求的正文发送,而不是 URL 编码吗?
-
@Pac0 实际上 API 只接受这种格式。我尝试了其他方法,但没有运气。此外,其他方法也以相同的方式传递数据。
标签: c# asp.net-mvc winforms asp.net-web-api json.net