[英]convert json string to c# list of objects (string comes from request as NULL)
我有以下代码将json字符串转换为对象列表:
public class rest_all
{
public string restaurants { get; set; }
}
public class rest_all_data
{
public string RestaurantName { get; set; }
public string CategoryName { get; set; }
public string FourSquareID { get; set; }
}
public class rest_collection
{
public IEnumerable<rest_all_data> rest_all_data { get; set; }
}
这是主要功能:
public void AddRestaurantMultiple (rest_all rest_all)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
rest_collection collection = serializer.Deserialize<rest_collection>(rest_all.restaurants);
}
问题是当我使用json字符串发出http请求时,如下所示:
{"restaurants" : [{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]
它总是在AddRestaurantMultiple
函数上给我null ...我在做什么错?
您的模型应该是
public class Restaurant
{
public string RestaurantName { get; set; }
public string CategoryName { get; set; }
public string FourSquareID { get; set; }
}
public class rest_collection
{
public List<Restaurant> restaurants { get; set; }
}
var result = new JavaScriptSerializer().Deserialize<rest_collection>(yourjson);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.