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; }
}
and here is the main function:
public void AddRestaurantMultiple (rest_all rest_all)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
rest_collection collection = serializer.Deserialize<rest_collection>(rest_all.restaurants);
}
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"}]
it always gives me null at the AddRestaurantMultiple
function...what is it am i doing wrong??
AbdallahAbdallah
asked Aug 11, 2015 at 16:37
1 Answer 1
Your model should be
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);
answered Aug 11, 2015 at 16:40
4 Comments
Abdallah
the problem is that when I do an http request using fiddler...the json string in the
AddRestaurantMultiple
always comes as nullEser
@Abdallah Given your json in question above code works correctly. I don't know what/how you are doing with fiddler.
Abdallah
this is the message i send using fiddler....
{"restaurants:[{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]}
Eser
@Abdallah I don't know your server, its API and what it expects as input to its methods. So I have no idea what you are doing...
lang-cs
AddRestaurantMultiple
function? Is this a client-side request for the JSON or server-side?AddRestaurantMultiple
function theresaurant
string inrest_all
class is always NULL