-1

I want to dynamically load JSON from a URL in C#

I tried this code, but not load the array:

using Newtonsoft.Json;

using (var webClient = new System.Net.WebClient())
{
    var url = "https://api.coinmarketcap.com/v1/ticker/808coin/"
    var json = webClient.DownloadString(url);
    dynamic array = JsonConvert.DeserializeObject(json);
    var nome = array.name.ToString();
    Label33.Text = nome;
}
Mark
  • 1,128
  • 13
  • 21

1 Answers1

-1

Using the following class definition, you can deserialize directly to C# values (as strings). From there, you can convert it down further, but this should preserve the values fine. Tested on my own machine and the values work fine.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TinkeroonieCSharp
{
    class UserJSON
    {
        [JsonProperty]
        public string id;
        [JsonProperty]
        public string name;
        [JsonProperty]
        public string symbol;
        [JsonProperty]
        public string rank;
        [JsonProperty]
        public string price_usd;
        [JsonProperty]
        public string price_btc;
        [JsonProperty]
        public string twenty_four_hour_volume_usd;
        [JsonProperty]
        public string market_cap_usd;
        [JsonProperty]
        public string available_supply;
        [JsonProperty]
        public string total_supply;
        [JsonProperty]
        public string max_supply;
        [JsonProperty]
        public string percent_change_1h;
        [JsonProperty]
        public string percent_change_24h;
        [JsonProperty]
        public string percent_change_7d;
        [JsonProperty]
        public string last_updated;
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var webClient = new System.Net.WebClient())
            {
                var json = webClient.DownloadString("https://api.coinmarketcap.com/v1/ticker/808coin/");
                List<UserJSON> array = JsonConvert.DeserializeObject<List<UserJSON>>(json);
                var name = array[0].name;
                Console.WriteLine(name);
            }
        }
    }
}

It's a little ugly but it gets the point across. Look over the following question HERE to have more info on the method I used!

The reason an error was occurring was because you were deserializing it down into the string of the entire JSON file, so attempting to index name would always return null, since it is just a string variable. By segmenting the deserialize down into each individual property of the JSON file, we can access anything we need without hassle at the point of deserialization. So you could go

thing = JsonConvert.DeserializeObject<UserJSON>(json).name

to cut down on your lines, if needed. Unadvised, though. Best cache it for later! :D

James Whyte
  • 788
  • 3
  • 14
  • Of course, you could deserialize right into a UserJSON variable instead of to a list. This method would be if you had called https://api.coinmarketcap.com/v1/ticker/ and received more than one entry to iterate over. :) – James Whyte Apr 18 '18 at 04:10
  • 1
    Thanks a lot James, your answer solved my problem! I only need to understand the code :-) – Odacir Cristofolini Apr 18 '18 at 13:08
  • Hummmm i need to try how to acess any of coins using this url: api.coinmarketcap.com/v1/ticker.... I tryed the line: thing = JsonConvert.DeserializeObject(json).name But not worked.... need to study better about this, but my problem was solved with your help. Thank you !!!!!! :-) – Odacir Cristofolini Apr 18 '18 at 13:10
  • I tryed to load a Json from this URL: https://www.worldcoinindex.com/apiservice/ticker?key=0b6dmpsEyLlR7meh1QfALwEGE6dC3J&label=cheesebtc&fiat=btc but not found a way. What i need to change to-do this? I create a new class with the elements, but not work. – Odacir Cristofolini Apr 25 '18 at 11:19