繁体   English   中英

似乎无法从数组反序列化为强类型列表对象

[英]cant seem to Deserialize to strongly typed list object from array

我正在尝试反序列化使用以下 Json 和类:

List<Root> OBJTEST = (List<Root>)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(List<Root>));

你那里没有数组值。 您应该反序列化到对象Root obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response)<\/code> 。

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "IBM",
    "3. Last Refreshed": "2022-02-04",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2022-02-04": {
        "1. open": "137.8600",
        "2. high": "138.8200",
        "3. low": "136.2150",
        "4. close": "137.1500",
        "5. volume": "4142045"
    },
    "2022-02-03": {
        "1. open": "137.0000",
        "2. high": "138.7600",
        "3. low": "135.8310",
        "4. close": "137.7800",
        "5. volume": "6100777"
    }
  }
}

试试这个

    string json = string.Empty;
    using (var client = new HttpClient())
    {
        var baseAddress = "https://www.alphavantage.co";
        var api = "/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo";

        client.BaseAddress = new Uri(baseAddress);
        var contentType = new MediaTypeWithQualityHeaderValue("application/json");
        client.DefaultRequestHeaders.Accept.Add(contentType);
        var response = client.GetAsync(api).Result;
        if (response.IsSuccessStatusCode)
            json = response.Content.ReadAsStringAsync().Result;
    }
    Data data =JsonConvert.DeserializeObject<Data>(json);

你可以得到 TimeSeriesDaily 列表

List<TimeSeriesDaily> timeSeriesDaily = data.TimeSeriesDaily.Select(tsd => AddDate(tsd) ).ToList();

private TimeSeriesDaily AddDate(KeyValuePair<string, TimeSeriesDaily> tsdd)
{
    var tsd=tsdd.Value;
    tsd.Date=tsdd.Key;
    return tsd;
}

班级

public partial class Data
    {
        [JsonProperty("Meta Data")]
        public MetaData MetaData { get; set; }

        [JsonProperty("Time Series (Daily)")]
        public Dictionary<string, TimeSeriesDaily> TimeSeriesDaily { get; set; }
    }

    public partial class MetaData
    {
        [JsonProperty("1. Information")]
        public string The1Information { get; set; }

        [JsonProperty("2. Symbol")]
        public string The2Symbol { get; set; }

        [JsonProperty("3. Last Refreshed")]
        public DateTimeOffset The3LastRefreshed { get; set; }

        [JsonProperty("4. Output Size")]
        public string The4OutputSize { get; set; }

        [JsonProperty("5. Time Zone")]
        public string The5TimeZone { get; set; }
    }

public partial class TimeSeriesDaily
{
    [JsonIgnore]
    public string Date { get; set; }
    
    [JsonProperty("1. open")]
    public string The1Open { get; set; }

    [JsonProperty("2. high")]
    public string The2High { get; set; }

    [JsonProperty("3. low")]
    public string The3Low { get; set; }

    [JsonProperty("4. close")]
    public string The4Close { get; set; }

    [JsonProperty("5. volume")]
    public long The5Volume { get; set; }
}

C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object>

[英]C# strongly typed ConfigurationBuilder List<object> from appsettings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将json反序列化为包含字典的强类型对象 无法将JSON反序列化为强类型对象 将 JSON 数组反序列化为强类型模型 C# 强类型ConfigurationBuilder列表<object>从应用设置<div id="text_translate"><p>真的在为这个苦苦挣扎——它应该这么难吗?!</p><p> 我的 appsettings 中有一个简单的 object 数组:</p><pre> "AppSettings": { "Names": [ { "Id": "1", "Name": "Mike" }, { "Id": "2", "Name": "John" } ] }</pre><p> 然后我有一个 class</p><pre> public class AppSettings { public List&lt;Names&gt; Names { get; set; } = new List&lt;Names&gt;(); } public class Names { public string Id { get; set; } public string Name { get; set; } }</pre><p> 我在我的应用设置中读到:</p><pre> var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json").Build(); var config = new AppSettings();</pre><p> 这就是出错的地方:</p><pre> config.Names = configuration.GetSection("AppSettings:Names") //&lt;&lt;&lt;&lt; what do I do here?</pre><p> 似乎它都与IConfigurationSection相关,这没有帮助。</p></div></object> 使用 BrokeredMessage 从 Azure 服务总线队列 (v1) 反序列化强类型对象 如何将具有固定架构的值数组反序列化为强类型数据类? 将2的JSON数组序列化为强类型对象 将 JSON 数组反序列化为强类型 .NET 对象 迭代数组并将强类型添加到列表 如何从MarkupExtension返回强类型对象?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM