-4

I have a web api service that converts JSON object to a specific list:

here is the model class:

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 List<rest_all_data> rest_all_data { get; set; }
} 

and here is the service:

public void AddRestaurantMultiple([FromBody] JObject rest_all)
{
    string k = rest_all.ToString();
    rest_collection result = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<rest_collection>(k); 
}

and here is the json object:

"restaurants" : [{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]

the rest_all object always comes with data and the k string is also a success but the result variable is always null...

10
  • 3
    Why are you calling JObject.ToString? It seems odd to format the JSON only to parse it again... And what does the JSON look like? Commented Aug 12, 2015 at 12:53
  • here what the json object looks like "restaurants" : [{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}] Commented Aug 12, 2015 at 12:56
  • Put it in the question, preferably making the whole thing a short but complete program demonstrating the problem, just like I asked you to last time... Commented Aug 12, 2015 at 12:58
  • @JonSkeet...i added it...do u have any solution to my problem? Commented Aug 12, 2015 at 13:21
  • Well you haven't added a short but complete program demonstrating the problem, and that JSON isn't an object - it doesn't start with { and end with }. Oh, and then there's the matter that your collection seems to be called restaurants, not rest_all_data. Commented Aug 12, 2015 at 13:32

2 Answers 2

1

try it i have made few changes in your code

    public class rest_collection
    {
        public IEnumerable<rest_all_data> rest_all_datas { get; set; }
    }

    public void AddRestaurantMultiple([FromBody] JObject rest_all)
    {
        string k = rest_all.ToString();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        rest_collection collection = serializer.Deserialize<rest_collection>(k);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try:

public void AddRestaurantMultiple([FromBody] string rest_all)
{
    var obj = JsonConvert.DeserializeObject<rest_collection>(rest_all);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.