0

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

Clemens
  • 123,504
  • 12
  • 155
  • 268
Toni0123
  • 107
  • 1
  • 11
  • 1
    How does your json look like? Seems that it was not generated correctly – Alexander Powolozki Aug 16 '19 at 08:02
  • @AlexanderPowolozki I have added json. json2csharp.com/ gives me that output. – Toni0123 Aug 16 '19 at 08:17
  • 1
    In case you are using Newtonsoft, here my is example how to parse and access JSON like your https://stackoverflow.com/questions/57072459/how-to-get-a-value-from-a-json-object-in-c-sharp/57461616#57461616 – Jan Aug 16 '19 at 08:29

1 Answers1

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

JeePakaJP
  • 830
  • 7
  • 21
  • Thanks, will try this. Do you have any good link for your code? – Toni0123 Aug 16 '19 at 08:53
  • can't install QuickType in VS2019 "This extension is not installable on any currently installed products" – Toni0123 Aug 16 '19 at 09:25
  • You don't need to install Quicktype, just past your json on his website, and generate C# – JeePakaJP Aug 16 '19 at 09:59
  • This part "public static Crypto FromJson(string json) => JsonConvert.DeserializeObject(json, QuickType.Converter.Settings); " gives me error "QuickType missing" – Toni0123 Aug 16 '19 at 10:21