C# 无法将当前JSON对象(例如,{quot;name";:“value";})反序列化为类型';System.Collections.Generic.List

C# 无法将当前JSON对象(例如,{quot;name";:“value";})反序列化为类型';System.Collections.Generic.List,c#,.net,json,asp.net-mvc,C#,.net,Json,Asp.net Mvc,我有以下代码 public async Task<ActionResult> GetExtendedProperties() { Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint); var token = AppToken.GetAppToken(); var adClient = AuthenticationHel

我有以下代码

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();
            }
        }
使用json2charp,我创建了这个类:

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; }
    }
公共类ActiveDirectorySchemaExtension
{
公共字符串对象类型{get;set;}
公共字符串objectId{get;set;}
公共对象删除时间戳{get;set;}
公共字符串appDisplayName{get;set;}
公共字符串名称{get;set;}
公共字符串数据类型{get;set;}
公共布尔值从内部部署{get;set;}同步
公共列表目标对象{get;set;}
}
如何将该字符串转换为列表,以便在视图上轻松操作它

更新: 我试过这个:

 List<ActiveDirectorySchemaExtension> tmp = JsonConvert.DeserializeObject<List<ActiveDirectorySchemaExtension>>(jsonresult);
List tmp=JsonConvert.DeserializeObject(jsonresult);
但我有这个

无法反序列化当前JSON对象(例如{“名称”:“值”}) 打字 'System.Collections.Generic.List`1[CapatechSaaApp.Areas.GlobalAdmin.Models.ActiveDirectorySchemaExtension]' 因为该类型需要一个JSON数组(例如[1,2,3])来反序列化 正确地要修复此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型,使其成为正常类型 .NET类型(例如,不是integer之类的基元类型,也不是集合 可以从JSON反序列化的类型(如数组或列表) 对象还可以将JsonObjectAttribute添加到类型以强制它 从JSON对象反序列化。路径“['odata.metadata']”,第1行, 立场18


您可以使用DataContractSerializer

var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var root = serializer.ReadObject(ms) as RootObject;
但您也应该用[DataContract]属性标记类,用[DataMember]属性标记成员

像这样:

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

您忘记了json2csharp中的父对象


可能重复的请看一点也不重复,问题很具体,反序列化将转换为一个对象,我需要转换为一个对象列表,本质上是一样的。请记住,
List
也是一个对象。如果该对象不起作用,请参阅更新
 [DataContract]
    public class RootObject
    {
        [DataMember(Name = "odata.metadata")]
        public string metadata { get; set; }
        [DataMember]
        public List<ActiveDirectorySchemaExtension> value { get; set; }
    }
[DataMember(Name = "odata.type")]
public string odataType { get; set; }
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);
}
extension_33e037a7b1aa42ab96936c22d01ca338_Modulos