将json字符串转换为c#对象列表(字符串来自请求为NULL)
本文关键字:字符串 请求 NULL 对象 json 转换 列表 | 更新日期: 2023-09-27 18:07:24
我有以下代码将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);
}
的问题是,当我做一个http请求与json字符串像这样:
{"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);