0

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??

Abdallah
  • 523
  • 2
  • 7
  • 15
  • possible duplicate of [Convert Json String to C# Object List](http://stackoverflow.com/questions/22191167/convert-json-string-to-c-sharp-object-list) – Dipen Shah Aug 11 '15 at 16:38
  • 2
    What do you mean by "it always gives me a null"? That's very unclear. It would help if you could provide a short but *complete* program demonstrating the problem, ideally using idiomatic .NET naming... – Jon Skeet Aug 11 '15 at 16:39
  • How are you "calling" to the class that contains the `AddRestaurantMultiple` function? Is this a client-side request for the JSON or server-side? – Karl Anderson Aug 11 '15 at 16:42
  • I am using fiddler to generate the request...and by null i mean when i send a request to the input of `AddRestaurantMultiple` function the `resaurant` string in `rest_all` class is always NULL – Abdallah Aug 12 '15 at 09:22

1 Answers1

4

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);
Eser
  • 12,346
  • 1
  • 22
  • 32
  • the problem is that when I do an http request using fiddler...the json string in the `AddRestaurantMultiple` always comes as null – Abdallah Aug 12 '15 at 09:10
  • @Abdallah Given your json in question above code works correctly. I don't know what/how you are doing with fiddler. – Eser Aug 12 '15 at 09:16
  • this is the message i send using fiddler.... `{"restaurants:[{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]}` – Abdallah Aug 12 '15 at 09:33
  • @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... – Eser Aug 12 '15 at 09:35