JSON字符串转换为对象的C#列表(字符串来自请求为NULL) [英] convert json string to c# list of objects (string comes from request as NULL)
本文介绍了JSON字符串转换为对象的C#列表(字符串来自请求为NULL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code到JSON字符串转换成列表中的对象:
I have the following code to convert json string to list of objects:
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请求:
the problem is that when I make an http request with a json string like this:
{"restaurants" : [{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]
它总是给我空的 AddRestaurantMultiple
函数...它是什么我做错了?
it always gives me null at the AddRestaurantMultiple
function...what is it am i doing wrong??
推荐答案
您模式应该是
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);
这篇关于JSON字符串转换为对象的C#列表(字符串来自请求为NULL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文