Home > Net >  C# getting json with array in it from Cosmos db
C# getting json with array in it from Cosmos db

Time:02-23

I need to retrieve the following JSON from my CosmosDb, but I think the C# code (which works for a simple JSON structure), needs help. The response just shows

{ RolePermission[0]  } 



var response = this.client.CreateDocumentQuery<RolePermission>(
                       UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions)
                       .Where(f => f.RoleName == roleName).AsEnumerable().ToArray();

and the JSON is:

{
"id": "1",
"RoleName": "Admin",
"Menus": [
    {
        "Item": "Admin",
        "Display": "Admin",
        "Granted": true
    },
    {
        "Item": "Users",
        "Display": "Users",
        "Granted": true
    }
],
}

The Class defn, I want to get it into is:

public class RolePermission
{
    [DisplayName("Role Name")]
    public string RoleName { get; set; }
    public List<Menus> Menus { get; set; }
}

public class Menus
{
    public string Item { get; set; }
    public string Display { get; set; }
    public bool Granted { get; set; }
}

I just need the menus part of the JSON

Thanks

CodePudding user response:

According to the data that you provided and the code , it should return the list you need "Menus" with the below query

var response = this.client.CreateDocumentQuery<RolePermission>(UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions).Where(f => f.RoleName == roleName).ToList(); 

Make sure you have the correct data inserted with the field RoleName in the container.

  • Related