2
votes

I have the following code

public async Task<ActionResult> GetExtendedProperties()
        {
            Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint);
            var token = AppToken.GetAppToken();
            var adClient = AuthenticationHelper.GetActiveDirectoryClient();

            Microsoft.Azure.ActiveDirectory.GraphClient.Application app = (Microsoft.Azure.ActiveDirectory.GraphClient.Application)adClient.Applications.Where(
                a => a.AppId == SettingsHelper.ClientId).ExecuteSingleAsync().Result;
            if (app == null)
            {
                throw new ApplicationException("Unable to get a reference to application in Azure AD.");
            }

            string requestUrl = string.Format("https://graph.windows.net/{0}/applications/{1}/extensionProperties?api-version=1.5", SettingsHelper.Tenant, app.ObjectId);

            HttpClient hc = new HttpClient();
            hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                "Bearer", token);

            HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));

            if (hrm.IsSuccessStatusCode)
            {
                string jsonresult = await hrm.Content.ReadAsStringAsync();


                return View();
            }
            else
            {
                return View();
            }
        }

and it returns this string

{"odata.metadata":"https://graph.windows.net/mysaasapp.onmicrosoft.com/$metadata#directoryObjects/Microsoft.DirectoryServices.ExtensionProperty","value":[{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"f751a646-2cc1-4e30-bfc6-a8217c0ce0a3","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Modulos","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"80aabe1b-b020-41d1-bd2d-cc04af264fe5","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_ModulesPerUser","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"6e3d7592-7a66-4792-b408-891251197868","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Comasdasa3dsdaspInfo","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"93a26374-4135-4f29-9f24-4154522449ec","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"21a8a3d4-f4b4-45b4-8d07-55d450db35f2","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_CompanyNameForSaasApp","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"7b3109e0-8710-4d1a-81c3-2b6a83fb62ee","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Compania","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]}]}

Using json2charp, I created this class:

public class ActiveDirectorySchemaExtension
    {
        public string objectType { get; set; }
        public string objectId { get; set; }
        public object deletionTimestamp { get; set; }
        public string appDisplayName { get; set; }
        public string name { get; set; }
        public string dataType { get; set; }
        public bool isSyncedFromOnPremises { get; set; }
        public List<string> targetObjects { get; set; }
    }

How can I convert that string into a List so that I can easy manipulate it on the view?

Update: I tried this:

 List<ActiveDirectorySchemaExtension> tmp = JsonConvert.DeserializeObject<List<ActiveDirectorySchemaExtension>>(jsonresult);

but I got this

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CapatechSaasApp.Areas.GlobalAdmin.Models.ActiveDirectorySchemaExtension]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '['odata.metadata']', line 1, position 18.

2
possible duplicate of Newtonsoft JSON DeserializeDPac
Please see duplicateDPac
not duplicated at all, the question is very specific, deserialize will convert to an object, I need to convert to a List of objectsLuis Valencia
It is essentially the same thing. Remember that List is also an object.DPac
that is not working, please see UpdateLuis Valencia

2 Answers

3
votes

You forgot your parent object in json2csharp.

Demo on .NETFiddle

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class ActiveDirectorySchemaExtension // You can switch from the original class name to yours
{
    public string Type { get; set; }        // You should switch to PascalCase to respect C# notation
    public string ObjectType { get; set; }
    public string ObjectId { get; set; }
    public object DeletionTimestamp { get; set; }
    public string AppDisplayName { get; set; }
    public string Name { get; set; }
    public string DataType { get; set; }
    public bool IsSyncedFromOnPremises { get; set; }
    public List<string> TargetObjects { get; set; }
}

public class RootObject
{
    public string Metadata { get; set; }
    public List<ActiveDirectorySchemaExtension> Value { get; set; }
}

public void Main()
{
    var json = "{'odata.metadata':'https://graph.windows.net/mysaasapp.onmicrosoft.com/$metadata#directoryObjects/Microsoft.DirectoryServices.ExtensionProperty','value':[{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'f751a646-2cc1-4e30-bfc6-a8217c0ce0a3','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Modulos','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'80aabe1b-b020-41d1-bd2d-cc04af264fe5','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_ModulesPerUser','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'6e3d7592-7a66-4792-b408-891251197868','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Comasdasa3dsdaspInfo','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'93a26374-4135-4f29-9f24-4154522449ec','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'21a8a3d4-f4b4-45b4-8d07-55d450db35f2','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_CompanyNameForSaasApp','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'7b3109e0-8710-4d1a-81c3-2b6a83fb62ee','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Compania','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']}]}";
    var o = JsonConvert.DeserializeObject<RootObject>(json);
    Console.WriteLine(o.Value[0].Name);
}

output :

extension_33e037a7b1aa42ab96936c22d01ca338_Modulos
2
votes

You can use DataContractSerializer

var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var root = serializer.ReadObject(ms) as RootObject;

But also you should mark your classes with [DataContract] attribute and members with [DataMember] attribute.

Like this:

 [DataContract]
    public class RootObject
    {
        [DataMember(Name = "odata.metadata")]
        public string metadata { get; set; }
        [DataMember]
        public List<ActiveDirectorySchemaExtension> value { get; set; }
    }

Also, you've lost one field in your class ActiveDirectorySchemaExtension - odataType. You can deserialize it like this:

[DataMember(Name = "odata.type")]
public string odataType { get; set; }