【发布时间】:2013-06-27 23:09:16
【问题描述】:
所以我只有一个简单的 Web API,它返回如下 JSON 格式
{
"dailyDealId": "432",
"discountPercentage": "0",
"product": {
"productId": "10",
"brandId": "10",
"departmentId": "3",
"name": "Baby Girl Velour Tunic & Snowflake Legging Set",
"description": "The pretty set",
"url": "http://whatever.whatever.com/files/whatever.tif"
}
}
我想在我的 C# 控制台代码中获取这些数据
这是我的模型类 Data.cs
class Data
{
public string dailyDealId { get; set; }
public string discountPercentage { get; set; }
public Array product { get; set; }
}
这是我的主要代码
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://whatever.com/");
HttpResponseMessage response = client.GetAsync("product/").Result;
if (response.IsSuccessStatusCode)
{
var products = response.Content.ReadAsAsync<IEnumerable<Data>>().Result;
foreach (var p in products)
{
Console.WriteLine("dailyDealId" + p.dailyDealId);
}
}
}
但它似乎不起作用,我得到 Newtonsoft.Json.JsonSerializationException: 无法反序列化当前 JSON 错误,任何帮助将不胜感激
谢谢
【问题讨论】:
-
响应的内容是什么?
http://whatever.com/product/是否只返回 json 或您的网址有误? -
url 是正确的,所以当我使用 POSTMAN 或 Soap ui 调用 url 时,它会返回我放在那里的 JSON 数据
-
您正在调用
asynchronous方法并期望它在返回时阻塞.. -
那么你将如何解决它?
-
@SimonWhitehead 访问任务结果隐式等待。
标签: c# json rest asp.net-web-api json.net