Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have done this before and I usually am goof to determine what is wrong with my JSON object or my code, but I can't seem to understand what I am doing wrong. I have even used Jsontocharp.com and it gives me the result I expect.

Here is my json:
{"user":[{"ID":"12393","userID":"4772","username":"bullyboy","dtDate":"2020-11-02
17:26:54.277}],"actions":[{"actionsID":"23983","actionsName":"Clicked","ObjectName":"Button"},{"actionsID":"23984","actionsName":"Submitted","ObjectName":"Form"} ], "Deleted":[],"Forms":[],"Timestamp":"2020-11-06 13:45:28"}


my Root is:
public class Root
   {
       public List<Users> user { get; set; }
      public List<Actions> actions { get; set; }
       public List<DeletedData> Deleted { get; set; }
       public List<CMForms> Forms { get; set; }
       public string Timestamp { get; set; }
   }


my deserializer is as follows

response = client.PostAsync(url, params).Result;
                if(response.StatusCode == HttpStatusCode.OK)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    Root jsonList = JsonConvert.DeserializeObject<Root>(content);
                   foreach(var act in jsonList.actions)
                    {
                        Console.WriteLine(act);
                    }


I am getting the error message listed above. I'm not sure what I am doing incorrectly. By the way the empty lists are because nothing resulted for that user.I have to take that into account. If the list is empty I simply skip over it, but I can't even deserialize my list..

What I have tried:

I can't change what is being submitted to me aka the JSON so I I have tried making my Root as a list and it doesn't give me that room but that isn't what I want because what I am returning is not a list.. right? At least I am viewing it as an object and inside that object I have lists.
Posted
Updated 6-Nov-20 19:00pm
Comments
[no name] 6-Nov-20 19:43pm    
Without a "Root" in your JSON, it simply doesn't match what your class "says". Wrap a "Root" around it.
Member 14977918 6-Nov-20 19:58pm    
you mean wrap my json in a root? Isn't it already inside of a root? I tried adding like a "root":{ before the whole thing and I return another root that return a root which has all the data inside my current root.
[no name] 6-Nov-20 23:45pm    
MS recommends:

https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer?view=netcore-3.1
Member 14977918 7-Nov-20 1:01am    
Thank you, I figured it out. wasn't may json at all it was the parameters I was sending to get the data from the web service. Essentially I wasn't returning anything.

As it turns out my JSON layout wasn't wrong. I was using Postman to check the result of my web service which is what I wanted but in my code I wasn't passing thew necessary data to retrieve the results like I was doing in postman. Once I fixed that part everything else worked like a charm.
 
Share this answer
 
As it turns out my JSON layout wasn't wrong. I was using Postman to check the result of my web service which is what I wanted but in my code I wasn't passing thew necessary data to retrieve the results like I was doing in postman. Once I fixed that part everything else worked like a charm.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900