如何从属性数量未知的类中反序列化复杂的json [英] How to deserialize complex json from class that have unknown number of properties

查看:49
本文介绍了如何从属性数量未知的类中反序列化复杂的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有复杂的json,并且无法从中获取数据.

I have complex json and problem getting data from it.

我需要显示每日货币,但是我不确定如何从TimeSeriesDigitalCurrencyDaily类获取数据. TimeSeriesDigitalCurrencyDaily类可以具有N个属性.

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.

如何摆脱"__invalid_type__20171214153500 __invalid_name__2017-12-14 15:35:00"并为此属性和所有其他属性指定合适的名称?

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

推荐答案

您可以尝试使用

You sould try to serialise with url (Quicktype)

检查此输出

// <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 }
            },
        };
    }
}

这篇关于如何从属性数量未知的类中反序列化复杂的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆