Dynamically load Json from URL on C#

不羁岁月 提交于 2019-12-13 09:34:49

问题


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

回答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



来源:https://stackoverflow.com/questions/49890875/dynamically-load-json-from-url-on-c-sharp

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