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

asked Aug 11, 2015 at 16:37
4
  • possible duplicate of Convert Json String to C# Object List Commented Aug 11, 2015 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... Commented Aug 11, 2015 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? Commented Aug 11, 2015 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 Commented Aug 12, 2015 at 9:22

1 Answer 1

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);
answered Aug 11, 2015 at 16:40

4 Comments

the problem is that when I do an http request using fiddler...the json string in the AddRestaurantMultiple always comes as null
@Abdallah Given your json in question above code works correctly. I don't know what/how you are doing with fiddler.
this is the message i send using fiddler.... {"restaurants:[{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]}
@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...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.