我无法反序列化我的JSON。看起来不错。 Fiddler解析它,但我的实体看起来被卡在数据字段中。如何从“数据”字段中获取数据?我是在PCL内部这样做的,这是第一次这样做,通常我在JQuery中做过这个,从来没有任何问题。
从REST服务返回JSON:
{"ContentEncoding":null,"ContentType":null,"Data":[{"BusinessId":2,"CommunityId":2,"BusinessName":"TestBusiness1","Address1":"12 1st Ave.","Address2":"a","City":"Fargo","State":"ND","Zip":"58102","Phone":"7019283980","Email":"b1@falala.com","WebsiteURL":"google.com","Description":"First Test Business","Active":true,"HoursOfOperation":"Mon-Fri 8am-4pm","SelectedBusinessCategories":[4],"SelectedBusinessTypes":[2,3]},{"BusinessId":8,"CommunityId":2,"BusinessName":"TestHotel1","Address1":"600 50th St.","Address2":"a","City":"Fargo","State":"ND","Zip":"58103","Phone":"7013291780","Email":"b1@falala.com","WebsiteURL":"bing.com","Description":"First Test Hotel 4 boop","Active":true,"HoursOfOperation":"Mon-Fri 8am-4pm","SelectedBusinessCategories":[7],"SelectedBusinessTypes":[2]},{"BusinessId":9,"CommunityId":2,"BusinessName":"TestShop1","Address1":"349 32nd Ave.","Address2":"a","City":"Fargo","State":"ND","Zip":"58103","Phone":"7013298109","Email":"b1@falala.com","WebsiteURL":"something.com","Description":"First Test Shop 2","Active":true,"HoursOfOperation":"Mon-Fri 8am-4pm","SelectedBusinessCategories":[6],"SelectedBusinessTypes":[2]},{"BusinessId":10,"CommunityId":2,"BusinessName":"TestBusiness2","Address1":"24345 street","Address2":"a","City":"Fargo","State":"ND","Zip":"58104","Phone":"7014298490","Email":"b1@falala.com","WebsiteURL":"bing.com","Description":"Second Test Business","Active":true,"HoursOfOperation":"Mon-Fri 8am-4pm","SelectedBusinessCategories":[5],"SelectedBusinessTypes":[3]}],"JsonRequestBehavior":0,"MaxJsonLength":null,"RecursionLimit":null}
本地对象:
public class Item
{
public bool Active { get; set; }
public int ItemId { get; set; }
public int CommunityId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string WebsiteURL { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Email { get; set; }
public string Description { get; set; }
public int Category { get; set; }
}
public class Business : Item
{
public int BusinessId { get { return this.ItemId; } set { this.ItemId = value; } }
public string BusinessName { get { return this.Name; } set { this.Name = value; } }
public int[] SelectedBusinessCategories { get; set; }
public int[] SelectedBusinessTypes { get; set; }
public string HoursOfOperation { get; set; }
}
调用REST服务并反序列化JSON:
List<Business> items = new List<Business>();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(WebServiceURL);
var response = await client.GetAsync(Controller);
var content = await response.Content.ReadAsStringAsync();
dynamic datalist = JsonConvert.DeserializeObject<List<Business>>(content);
休息服务方法:
public JsonResult Get()
{
List<Business> businesses = new BusinessContext().Businesses.Where(b => b.Active == true).ToList();
JsonResult json = new JsonResult();
json.Data = businesses;
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return json;
}
答案 0 :(得分:1)
这是因为你的模型应该是
public class RootObject
{
public int BusinessId { get; set; }
public int CommunityId { get; set; }
public string BusinessName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string WebsiteURL { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
public string HoursOfOperation { get; set; }
public List<int> SelectedBusinessCategories { get; set; }
public List<int> SelectedBusinessTypes { get; set; }
}