将每个项目用作对象时,将json转换为C#类

问题描述 投票:1回答:2

如何为这种json创建模型,在属性中有数字,在对象上有日期等...

{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "AAP",
        "3. Last Refreshed": "2018-03-23",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2018-03-23": {
            "1. open": "112.4100",
            "2. high": "113.2600",
            "3. low": "110.3400",
            "4. close": "110.8400",
            "5. volume": "1085896"
        },
        "2018-03-22": {
            "1. open": "114.0200",
            "2. high": "115.1400",
            "3. low": "111.6300",
            "4. close": "111.7100",
            "5. volume": "1038170"
        }
    }
}

看看我会得到这种json:

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

c# json
2个回答
3
投票

终于创造了这个,它可以帮助任何人

public class StockViewModel
{
    [JsonProperty(PropertyName = "Meta Data")]
    public MetaData metaData { get; set; }

    [JsonProperty(PropertyName = "Time Series (Daily)")] 
    public Dictionary<DateTime, TimeSeriesIntraDayJsonClass> Data { get; set; }
}

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

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

    [JsonProperty(PropertyName = "3. Last Refreshed")]
    public DateTime LastRefreshed { get; set; }

    [JsonProperty(PropertyName = "4. Interval")]
    public string Interval { get; set; }

    [JsonProperty(PropertyName = "5. Output Size")]
    public string OutputSize { get; set; }

    [JsonProperty(PropertyName = "6. Time Zone")]
    public string TimeZone { get; set; }
}

public class TimeSeriesIntraDayJsonClass
{
    [JsonProperty(PropertyName = "1. open")]
    public double open { get; set; }
    [JsonProperty(PropertyName = "2. high")]
    public double high { get; set; }
    [JsonProperty(PropertyName = "3. low")]
    public double low { get; set; }
    [JsonProperty(PropertyName = "4. close")]
    public double close { get; set; }
    [JsonProperty(PropertyName = "5. volume")]
    public double volume { get; set; }
}

0
投票

我知道这是一个老问题,但也许这可以帮助其他任何想要将这些荒谬的json类型转换为分类的人。尽管可以改进属性的命名,但这证明可以方便地将任何人指向正确的方向

https://app.quicktype.io/

public class StockShares
{
    [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
{
    [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")]
    [JsonConverter(typeof(ParseStringConverter))]
    public long The5Volume { get; set; }
}   
© www.soinside.com 2019 - 2024. All rights reserved.