Search code examples
c#arraysjsonlinqobject

deserialize json in c# from the web in a c# console app


Basically I am trying to make the class I made for the object be able to read out indivual items so I can use them. I am having trouble with the foreach loop

Here is a copy of the Json I want to read

//    "roles": [
            //      {
            //        "name": "All Modules",
            //          "oid": "G|2",
            //          "uuid": "06be0407-53f9-4a0a-9b97-e771631e8d83",
            //          "study": true,
            //          "site": false,
            //          "href": "coming soon"
            //      }

here is the class im using

namespace SomeNamespace
{
    public class Roles
    {
        [JsonProperty("name")]
        public string name { get; set; }

        
    }

    public class RolesJson
    {
        [JsonProperty("roles")]
        public Roles roles { get; set; }
    }
}

Here is my RequestExample.cs

var outputString = $"{await response.Content.ReadAsStringAsync()}";


            //Console.WriteLine(outputString);
            
            JObject stuff = JObject.Parse(outputString);
            Console.WriteLine(stuff);
            //The Response body is the following:
            //{
            //    "roles": [
            //      {
            //        "name": "All Modules",
            //          "oid": "G|2",
            //          "uuid": "06be0407-53f9-4a0a-9b97-e771631e8d83",
            //          "study": true,
            //          "site": false,
            //          "href": "coming soon"
            //      }
            //            ]
            //}
            foreach (JObject jo in stuff["roles"])
            {
                foreach (var item in jo)
                {
                    Console.WriteLine(item);

                }
                // Console.WriteLine(jo.ToString());
                //prints this
                //{
                //    "name": "All Modules",
                //    "oid": "G|2",
                //    "uuid": "06be0407-53f9-4a0a-9b97-e771631e8d83",
                //    "study": true,
                //    "site": false,
                //    "href": "coming soon"
                //}
            }

Solution

  • you can use this website https://app.quicktype.io/ to create classes

        public partial class Data
        {
            [JsonProperty("roles")]
            public List<Role> Roles { get; set; }
        }
    
        public partial class Role
        {
            [JsonProperty("name")]
            public string Name { get; set; }
    
            [JsonProperty("oid")]
            public string Oid { get; set; }
    
            [JsonProperty("uuid")]
            public Guid Uuid { get; set; }
    
            [JsonProperty("study")]
            public bool Study { get; set; }
    
            [JsonProperty("site")]
            public bool Site { get; set; }
    
            [JsonProperty("href")]
            public string Href { get; set; }
        }
    

    and deserialize your data

        List<Role> roles=JsonConvert.DeserializeObject<Data>(json).Roles;
    
         Role role = roles[0];
    
         string name = role.Name;