【问题标题】:How to use StreamReader to read json file? [duplicate]如何使用 StreamReader 读取 json 文件? [复制]
【发布时间】:2019-10-02 03:40:40
【问题描述】:

如果我将 json 文件放在项目文件夹中,我可以读取它,但如果我将它放在像 http://111.111.111.111/test/test.json 这样的 url 路径上,我就无法读取它

try
{
  string filePath = @"http://111.111.111.111/test/test.json";

  //this way works
  // using (StreamReader r = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "test.json"))
  using ( StreamReader r = new StreamReader(filePath) )
  {
    string json = r.ReadToEnd();
    var items = JsonConvert.DeserializeObject<Item>(json);
  }
}
catch
{
}

【问题讨论】:

    标签: c# json


    【解决方案1】:

    这是不是以下内容的副本:Receiving JSON data back from HTTP request

    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    

    是否需要使用StreamReader

    无论如何,您都需要发出 HTTP 请求来检索数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 2014-04-19
      相关资源
      最近更新 更多