如何使用httpwebrequest使用c#从json api获取数据? [英] How to get data from json api with c# using httpwebrequest?

查看:33
本文介绍了如何使用httpwebrequest使用c#从json api获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 https://api.coinmarketcap.com/v1/ticker/获取所有变量 在我的 c# 控制台应用程序中.我该怎么做?

I want to get all variables from https://api.coinmarketcap.com/v1/ticker/ in my c# console application. How can I do this?

我开始将整个页面作为一个流.现在该怎么办?

I started with getting the whole page as a stream. What to do now?

private static void start_get()
{
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
        (string.Format("https://api.coinmarketcap.com/v1/ticker/"));

    WebReq.Method = "GET";

    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);

    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);
    Console.WriteLine(_Answer.ReadToEnd());
}

推荐答案

首先你需要一个用于反序列化的自定义类:

First you need a custom class to use for deserialization:

public class Item
{
    public string id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public string rank { get; set; }
    public string price_usd { get; set; }
    [JsonProperty(PropertyName = "24h_volume_usd")]   //since in c# variable names cannot begin with a number, you will need to use an alternate name to deserialize
    public string volume_usd_24h { get; set; }
    public string market_cap_usd { get; set; }
    public string available_supply { get; set; }
    public string total_supply { get; set; }
    public string percent_change_1h { get; set; }
    public string percent_change_24h { get; set; }
    public string percent_change_7d { get; set; }
    public string last_updated { get; set; }
}

接下来,您可以通过以下方式使用 Newtonsoft Json,一个免费的 JSON 序列化和反序列化框架获取您的项目(包括以下 using 语句):

Next, you can use Newtonsoft Json, a free JSON serialization and deserialization framework in the following way to get your items (include the following using statements):

using System.Net;
using System.IO;
using Newtonsoft.Json;

private static void start_get()
{
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("https://api.coinmarketcap.com/v1/ticker/"));

    WebReq.Method = "GET";

    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);

    string jsonString;
    using (Stream stream = WebResp.GetResponseStream())   //modified from your code since the using statement disposes the stream automatically when done
    {
       StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
       jsonString = reader.ReadToEnd();
    }

    List<Item> items = JsonConvert.DeserializeObject<List<Item>>(jsonString);

    Console.WriteLine(items.Count);     //returns 921, the number of items on that page
}

最后,元素列表存储在items中.

Finally, the list of elements is stored in items.

这篇关于如何使用httpwebrequest使用c#从json api获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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