How to deserialize complex json from class that have unknown number of properties

ぃ、小莉子 提交于 2021-01-29 07:59:44

问题


I have complex json and problem getting data from it.

I need to display daily currency, but I am not sure how to get data from TimeSeriesDigitalCurrencyDaily class. TimeSeriesDigitalCurrencyDaily class can have N number of properties.

How to get rid of "__invalid_type__20171214153500 __invalid_name__2017-12-14 15:35:00" and have proper name for this and all others properties?

  public class MetaData
{
    public string __invalid_name__1. Information { get; set; }
    public string __invalid_name__2. Digital Currency Code { get; set; }
    public string __invalid_name__3. Digital Currency Name { get; set; }
    public string __invalid_name__4. Market Code { get; set; }
    public string __invalid_name__5. Market Name { get; set; }
    public string __invalid_name__6. Interval { get; set; }
    public string __invalid_name__7. Last Refreshed { get; set; }
    public string __invalid_name__8. Time Zone { get; set; }
}

public class __invalid_type__20171214153500
{
    public string __invalid_name__1a. price (USD) { get; set; }
    public string __invalid_name__1b. price (USD) { get; set; }
    public string __invalid_name__2. volume { get; set; }
    public string __invalid_name__3. market cap (USD) { get; set; }
}

public class __invalid_type__20171214153000
{
    public string __invalid_name__1a. price (USD) { get; set; }
    public string __invalid_name__1b. price (USD) { get; set; }
    public string __invalid_name__2. volume { get; set; }
    public string __invalid_name__3. market cap (USD) { get; set; }
}

public class TimeSeriesDigitalCurrencyDaily
{
    public __invalid_type__20171214153500 __invalid_name__2017-12-14 15:35:00 { get; set; }
    public __invalid_type__20171214153000 __invalid_name__2017-12-14 15:30:00 { get; set; }
}

public class RootObject
{
    public MetaData __invalid_name__Meta Data { get; set; }
    public TimeSeriesDigitalCurrencyDaily __invalid_name__Time Series (Digital Currency Daily) { get; set; }
}

Json: https://www.alphavantage.co/query?function=DIGITAL_CURRENCY_DAILY&symbol=BTC&market=CNY&apikey=demo

{  
   "Meta Data":{  
      "1. Information":"Intraday Prices and Volumes for Digital Currency",
      "2. Digital Currency Code":"BTC",
      "3. Digital Currency Name":"Bitcoin",
      "4. Market Code":"USD",
      "5. Market Name":"United States Dollar",
      "6. Interval":"5min",
      "7. Last Refreshed":"2017-12-14 15:35:00",
      "8. Time Zone":"UTC"
   },
   "Time Series (Digital Currency Daily)":{  
      "2017-12-14 15:35:00":{  
         "1a. price (USD)":"16306.55330865",
         "1b. price (USD)":"16306.55330865",
         "2. volume":"140691.10869917",
         "3. market cap (USD)":"2294187064.05620003"
      },
      "2017-12-14 15:30:00":{  
         "1a. price (USD)":"16307.53476936",
         "1b. price (USD)":"16307.53476936",
         "2. volume":"140846.23060976",
         "3. market cap (USD)":"2296854802.80179977"
      }
   }
}

Any suggestion, links would be great. Thanks


回答1:


You sould try to serialise with url (Quicktype)

Check this output

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var crypto = Crypto.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

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

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

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

        [JsonProperty("2. Digital Currency Code")]
        public string The2DigitalCurrencyCode { get; set; }

        [JsonProperty("3. Digital Currency Name")]
        public string The3DigitalCurrencyName { get; set; }

        [JsonProperty("4. Market Code")]
        public string The4MarketCode { get; set; }

        [JsonProperty("5. Market Name")]
        public string The5MarketName { get; set; }

        [JsonProperty("6. Last Refreshed")]
        public string The6LastRefreshed { get; set; }

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

    public partial class TimeSeriesDigitalCurrencyDaily
    {
        [JsonProperty("1a. open (CNY)")]
        public string The1AOpenCny { get; set; }

        [JsonProperty("1b. open (USD)")]
        public string The1BOpenUsd { get; set; }

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

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

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

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

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

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

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

        [JsonProperty("6. market cap (USD)")]
        public string The6MarketCapUsd { get; set; }
    }

    public partial class Crypto
    {
        public static Crypto FromJson(string json) => JsonConvert.DeserializeObject<Crypto>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Crypto self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}



来源:https://stackoverflow.com/questions/57520934/how-to-deserialize-complex-json-from-class-that-have-unknown-number-of-propertie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!