从C#上的URL动态加载Json [英] Dynamically load Json from URL on C#

查看:99
本文介绍了从C#上的URL动态加载Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#中的URL动态加载JSON

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

推荐答案

使用以下类定义,您可以直接反序列化为C#值(作为字符串).从那里,您可以将其进一步向下转换,但是这样可以很好地保留这些值.在我自己的机器上进行了测试,并且值可以正常工作.

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

这有点丑陋,但却能说明要点.查看以下问题 HERE ,以获取有关该方法的更多信息用过!

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!

发生错误的原因是因为您将其反序列化为整个JSON文件的字符串,因此尝试索引名称始终将返回null,因为它只是一个字符串变量.通过将反序列化细分为JSON文件的每个单独属性,我们可以在反序列化时轻松地访问所需的任何内容.所以你可以去

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

根据需要减少行数.不过不建议.最好将其缓存以备后用! :D

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

这篇关于从C#上的URL动态加载Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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