2014-10-20 71 views
1

我的控制器方法如下反序列化的Web API项目JSON数组

public ActionResult Index() 
{ 
    Tweets model = null; 
    var client = new HttpClient(); 
    var task = client.GetAsync("http://localhost:33615/api/product").ContinueWith((t) => 
    { 
     var response = t.Result; 
     var readtask = response.Content.ReadAsAsync<Tweets>(); 
     readtask.Wait(); 
     model = readtask.Result; 
    }); 
    task.Wait(); 
    return View(model.Result); 
} 

的URL返回行作为follws:

[{"Id":1,"Name":"Honda Civic","Description":"Luxury Model 2013"},{"Id":2,"Name":"Honda Accord","Description":"Deluxe Model 2012"},{"Id":3,"Name":"BMW V6","Description":"V6 Engine Luxury 2013"},{"Id":4,"Name":"Audi A8","Description":"V8 Engine 2013"},{"Id":5,"Name":"Mercedes M3","Description":"Basic Model 2013"}] 

我的推文和鸣叫类如下:

public class Tweets 
{ 
    public Tweet[] Result; 
} 
public class Tweet 
{ 
    [JsonProperty("Name")] 
    public string Name { get; set; } 
    [JsonProperty("Description")] 
    public string Description { get; set; } 
} 

我找不出它在哪里我犯了错误。它给我以下错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RestFiddlerTest.Controllers.Tweets' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly 

如何解决这个问题。

回答

2

我已经仔细检查了你的代码,并且还有一些问题。 首先,我没有看到你的逻辑去反序列化为Json,根据JSon对象,我猜你正在使用Newtonsoft.Json?

然后你有一个类Tweets其中包含一个Tweet对象的数组。 现在,当你创建一个小样本,如下所示:

var dataToSerialize = new Tweets(); 
dataToSerialize.Result = new [] { new Tweet { Description = "Desc", Name = "Name" } }; 
var data = JsonConvert.SerializeObject(dataToSerialize); 

这段代码的输出将是如下:

{"Result":[{"Id":null,"Name":"Name","Description":"Desc"}]} 

这是不一样的,你得到的输出来自您的WebAPI。

现在,当你使用Newtonsoft.Json,你的序列化对象时,你可能做这样的事情:

const string JSon = "[{\"Id\":1,\"Name\":\"Honda Civic\",\"Description\":\"Luxury Model 2013\"},{\"Id\":2,\"Name\":\"Honda Accord\",\"Description\":\"Deluxe Model 2012\"},{\"Id\":3,\"Name\":\"BMW V6\",\"Description\":\"V6 Engine Luxury 2013\"},{\"Id\":4,\"Name\":\"Audi A8\",\"Description\":\"V8 Engine 2013\"},{\"Id\":5,\"Name\":\"Mercedes M3\",\"Description\":\"Basic Model 2013\"}]"; 
var data = JsonConvert.DeserializeObject<Tweets>(JSon); 

注:我已经把JSON字符串中一个字符串,因为我没有没有时间写完整的WebAPI。

现在,这是行不通的,因为字符串我输入这也是您的控制器不回报是不是一个Tweets对象之一(还记得你缺少你的JSON字符串的Result?相反,这是一个Tweet对象的数组,这样你就可以像下面这样更改代码:

var data = JsonConvert.DeserializeObject<Tweet>(JSon); 

不幸的是,你会收到完全相同的错误,为什么是因为你试图反序列化对象转换成的目标?键入Tweet而您的对象是类型为Tweet的数组。

因此,修改代码如下所示:

var data = JsonConvert.DeserializeObject<Tweet[]>(JSon); 

现在,你要反序列化你的对象为正确的类型。

enter image description here

所以,我希望这个帖子会帮助你。

亲切的问候,

1

我用Newtonsoft.Json。

public ActionResult Index() 
    { 
     List<Tweet> model = null; 
     var client = new HttpClient(); 
     var task = client.GetAsync("http://localhost:33615/api/product").ContinueWith((t) => 
     { 
      var response = t.Result; 
      var readtask = response.Content.ReadAsAsync<List<Tweet>>(); 
      readtask.Wait(); 
      model = readtask.Result; 
     }); 
     task.Wait(); 
     return View(model); 
    }