【问题标题】:Deserialize JSON with dynamic Element name [duplicate]使用动态元素名称反序列化 JSON [重复]
【发布时间】:2020-11-03 15:02:53
【问题描述】:

我有一个我正在尝试反序列化的 JSON 文件,但是 items 数组中有一个标签正在更改,那么如何在类描述中指定它,请参阅下面的类描述。

当我反序列化时,我收到以下消息: GetRapidAPIDataForSymbols 中的错误:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.List`1[MarketDataProvider.YahooHistory+RapidAPIItem]”,因为该类型需要正确反序列化的 JSON 数组(例如 [1,2,3])。

The JSON:

{
    "meta": {
        "currency": "EUR",
        "symbol": "UZU.DE",
        "exchangeName": "GER",
        "instrumentType": "EQUITY",
        "firstTradeDate": 911462400,
        "regularMarketTime": 1604397503,
        "gmtoffset": 3600,
        "timezone": "CET",
        "exchangeTimezoneName": "Europe/Berlin",
        "regularMarketPrice": 50.2,
        "chartPreviousClose": 51,
        "priceHint": 2,
        "dataGranularity": "1d",
        "range": ""
    },
    "items": {
        "1340002800": {
            "date": "18-06-2012",
            "open": 19.05,
            "high": 19.05,
            "low": 19.05,
            "close": 19.05,
            "adjclose": 16.09
        },
        "1340089200": {
            "date": "19-06-2012",
            "open": 19.04,
            "high": 19.05,
            "low": 19.04,
            "close": 19.04,
            "adjclose": 16.09
        },
        "1340175600": {
            "date": "20-06-2012",
            "open": 19.04,
            "high": 19.04,
            "low": 19.04,
            "close": 19.04,
            "adjclose": 16.09
        },
        "1340262000": {
            "date": "21-06-2012",
            "open": 19.05,
            "high": 19.05,
            "low": 19.05,
            "close": 19.05,
            "adjclose": 16.09
        },
        "1604397503": {
            "date": "03-11-2020",
            "open": 50.6,
            "high": 50.6,
            "low": 50.2,
            "close": 50.2,
            "adjclose": 50.2
        }
    },
    "error": null
}
My classes:


public class Meta
{
    public string currency { get; set; }
    public string symbol { get; set; }
    public string exchangeName { get; set; }
    public string instrumentType { get; set; }
    public long? firstTradeDate { get; set; }
    public long? regularMarketTime { get; set; }
    public int? gmtoffset { get; set; }
    public string timezone { get; set; }
    public string exchangeTimezoneName { get; set; }
    public decimal? regularMarketPrice { get; set; }
    public decimal? chartPreviousClose { get; set; }
    public decimal? previousClose { get; set; }
    public int? scale { get; set; }
    public int? priceHint { get; set; }
    public string dataGranularity { get; set; }
    public string range { get; set; }
}

public class RapidAPIHistoryResponse
{
    [XmlElement("meta")]
    public Meta meta { get; set; }
    [XmlElement("items")]
    public List<RapidAPIItem> items { get; set; }
    public object error { get; set; }
}
public class RapidAPIItem
{
    string dateTag { get; set; }
    public YahooPrice item { get; set; }
}


My code:
var client = new RestClient("https://yahoo-finance15.p.rapidapi.com/api/yahoo/hi/history/UZU.DE/1d);
                var request = new RestRequest(Method.GET);
                request.AddHeader("x-rapidapi-host", "yahoo-finance15.p.rapidapi.com");
                request.AddHeader("x-rapidapi-key", StartParameters.RapidAPIKey);
                IRestResponse response = client.Execute(request);
                YahooHistory.RapidAPIHistoryResponse history = JsonConvert.DeserializeObject<YahooHistory.RapidAPIHistoryResponse>(response.Content);

【问题讨论】:

标签: c# json serialization


【解决方案1】:

只要你尊重物品词典的关键:

public Dictionary<string, Item> Items { get; set; }

在示例中:

 "items": {
    "1340002800": {
        //...
    },
    "1340089200": {
      //...
    },

1340002800 和 1340089200 是 C# 类型字典的键,如下所示:

Dictionary<string, T> myDictionary  

所以你需要key是一个字符串,然后是自定义对象。

这还可以让您对“列表”中的每个项目进行操作:

var myResult = yahooQuotes.Items.Where(w => w.Item.High - w.Item.Low > 5).Select(s => s.Key);

【讨论】:

    【解决方案2】:

    在您的 Json 中有“items”:{....} 使用“{}”您说 items 是一个对象。但是您希望“项目”成为一个数组“[]”。所以你的 Json 应该更像

    "items" : [{"1604397503": {
            "date": "03-11-2020",
            "open": 50.6,
            "high": 50.6,
            "low": 50.2,
            "close": 50.2,
            "adjclose": 50.2
        }},{"1340262000": {
            "date": "21-06-2012",
            "open": 19.05,
            "high": 19.05,
            "low": 19.05,
            "close": 19.05,
            "adjclose": 16.09
        },
    

    }]

    【讨论】:

    • 我不知何故怀疑 OP 是否可以重写雅虎的股票 API 以适应他的心血来潮......
    • 另外,这不起作用,因为在您建议的 JSON 中,属性名称 "1604397503" 仍然是可变的,需要使用字典。 OP的数据模型需要固定的属性名{"dateTag": "1340262000", "item" : { /* ... */ } }
    猜你喜欢
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多