【问题标题】:C# JSON URL get dataC# JSON URL 获取数据
【发布时间】:2020-05-30 00:05:23
【问题描述】:

我是 System.Net 的 C# 新手 我想要一种从这个网站 api 获取信息的方法:https://fn-api.glitch.me/api/aes 从它的 json 到 C# 字符串

到目前为止我有这个

我不知道如何获取每个项目以及将网址放在哪里(我对此真的很陌生)。

我想要一个字符串中的 url:

public class Data
{
    public string build { get; set; }
    public string netCL { get; set; }
    public string manifestID { get; set; }
    public string aes { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
}

【问题讨论】:

  • “我想要字符串中的 url”:string url = "https://fn-api.glitch.me/api/aes";。也许您想获取页面的内容?看看System.Net.WebClient(你没说你是.net core还是.net full framework

标签: c# json parsing get set


【解决方案1】:

好的,这就是你的处理方式。我向您展示了一个使用HttpClient 的示例,它首先从API 中读取内容,然后使用Newtonsoft 包对其进行反序列化。

HttpClient 类:

public class HttpClientFactory
{
  private string webServiceUrl = "https://fn-api.glitch.me/";

  public HttpClient CreateClient()
  {
    var client = new HttpClient();
    SetupClientDefaults(client);
    return client;
  }

  protected virtual void SetupClientDefaults(HttpClient client)
  {
    //This is global for all REST web service calls
    client.Timeout = TimeSpan.FromSeconds(60);
    client.BaseAddress = new Uri(webServiceUrl);
  }
}

你的模型类:

public class Data
{
  public string build { get; set; }
  public string netCL { get; set; }
  public string manifestID { get; set; }
  public string aes { get; set; }
}

public class RootObject
{
  public Data data { get; set; }
}

现在,您可以调用此类并创建HttpClient 的实例,如下所示:

public RootObject InvokeAPI()
{
  RootObject apiresponse = new RootObject();
  string result = string.Empty;
  HttpClientFactory clientFactory = new HttpClientFactory();
  var client = clientFactory.CreateClient();
  HttpResponseMessage response = client.GetAsync("api/aes").Result;
  if (response.IsSuccessStatusCode)
  {
    result = response.Content.ReadAsStringAsync().Result;
    apiresponse = JsonConvert.DeserializeObject<RootObject>(result);
  }
 return apiresponse;
}

希望对你有所帮助。

编辑:

根据您的代码,您需要在您的Button 点击上调用API

    private void metroButton2_Click_1(object sender, EventArgs e)
    {
        //You need to invoke the API method !!!!
        var apiresponse=InvokeAPI();
        metroTextBox1.Text = apiresponse.data.aes;
    }

请务必在代码中放置try-catch 块以进行错误处理。

【讨论】:

  • 这可能是一个愚蠢的问题,但我如何获得 aes?就像我想这样做一样:metroTextBox1.Text = ???;
  • @ProMasterBoy-Gaming 一旦你得到反序列化的对象,你可以这样做:metroTextBox1.Text=apiresponse.data.aes;
  • @ProMasterBoy-Gaming 您在此处对字符串进行反序列化:apiresponse = JsonConvert.DeserializeObject&lt;RootObject&gt;(result); 其中 apiresponse 的类型为 RootObject,来自您的 Model
  • 我应该把apiresponse = JsonConvert.DeserializeObject&lt;RootObject&gt;(result);放在哪里?
  • @ProMasterBoy-Gaming 我不太确定,因为我这里没有你的代码结构。你需要把它放在你通过HttpClient调用API的地方
【解决方案2】:

我建议使用像 RestSharp 这样的第三方库。它将为您提供一个易于使用的客户端,并自动转换为对象。

或者,您可以使用WebClient 并下载 JSON。使用 Json.NET 之类的东西可以让您将 JSON 反序列化为一个对象。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 请进一步解释(我只有 13 xD)
    猜你喜欢
    • 1970-01-01
    • 2019-01-19
    • 2016-08-06
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2014-03-13
    相关资源
    最近更新 更多