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

Abdallah
  • 523
  • 2
  • 7
  • 15
  • 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? – Jon Skeet Aug 12 '15 at 12:53
  • here what the json object looks like `"restaurants" : [{"RestaurantName":"a","CategoryName":"b","FourSquareID":"c"},{"RestaurantName":"d","CategoryName":"e","FourSquareID":"f"}]` – Abdallah Aug 12 '15 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](http://stackoverflow.com/questions/31947347)... – Jon Skeet Aug 12 '15 at 12:58
  • @JonSkeet...i added it...do u have any solution to my problem? – Abdallah Aug 12 '15 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`. – Jon Skeet Aug 12 '15 at 13:32
  • @JonSkeet...i think the problem is clear enough since many people got it...and the name of the collection doesn't matter as it is just a name for the object...plus the json objects is converted to a string correctly and just fine so i guess this make the json object fine – Abdallah Aug 12 '15 at 14:22
  • "and the name of the collection doesn't matter as it is just a name for the object" - it absolutely *does* matter - which is why tutorialplus.net's answer changes the name of your property. Just because some people are willing to try to answer an unclear question doesn't make it any clearer, nor does it excuse you from putting effort into asking a good question. Please read http://tinyurl.com/stack-hints before you next ask a question. – Jon Skeet Aug 12 '15 at 14:24
  • @JonSkeet...because you see it unclear doesn't mean it's unclear...it means you just didn't get it...and tutorialplus.net's didn't change any of my properties...he just didn't use one of them..Please take a good look before replying – Abdallah Aug 12 '15 at 14:56
  • I think we'll have to agree to disagree at this point. Just bear in mind that I do have pretty significant experience in answering questions - I know what's likely to make a question well-received vs poorly-received. Also bear in mind that if you ask lots of questions which are poorly-received, you'll be automatically banned from asking more questions until you improve the old ones. You've been given advice on how to make your questions better - of course, it's up to you whether or not you follow that advice. – Jon Skeet Aug 12 '15 at 14:58
  • being new to stackoverflow doesn't demolish my experiences which you know nothing about...please don't make assumptions – Abdallah Aug 12 '15 at 15:03

2 Answers2

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);
    }
0

try:

public void AddRestaurantMultiple([FromBody] string rest_all)
{
    var obj = JsonConvert.DeserializeObject<rest_collection>(rest_all);
}
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24