Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从JSON文件获取数据并在c中使用#_C#_Json_Api_Stream - Fatal编程技术网

C# 从JSON文件获取数据并在c中使用#

C# 从JSON文件获取数据并在c中使用#,c#,json,api,stream,C#,Json,Api,Stream,我想制作一个程序,从API生成的JSON文件中读取一些数据。 下面是该JSON文件的一个示例: {"block4o": { "id": 20153910, "name": "Block4o", "profileIconId": 616, "revisionDate": 1408783284000, "summonerLevel": 30 }} 我需要做的是从中获取例如id和名称。 以下是我目前的代码: using System; using System.Coll

我想制作一个程序,从API生成的JSON文件中读取一些数据。
下面是该JSON文件的一个示例:

{"block4o": {
   "id": 20153910,
   "name": "Block4o",
   "profileIconId": 616,
   "revisionDate": 1408783284000,
   "summonerLevel": 30
}}
我需要做的是从中获取例如id和名称。 以下是我目前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.IO;
using System.Net;


namespace ConsoleApplication30
{
    class Program
    {
        static void Main(string[] args)
        {
        }
        public void HowToMakeRequestsToHttpBasedServices()
        {
            Uri serviceUri = new Uri("https://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/Block4o?api_key=****");
            WebClient downloader = new WebClient();
            downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
            downloader.OpenReadAsync(serviceUri);
        }

        void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result;
                // Continue working with responseStream here...
            }
        }
    }
}

您可以使用NuGet安装Newtonsoft.Json并编写如下smth:

if (e.Error == null)
{
    string text = e.Result;
    var events = JsonConvert.DeserializeObject<List<Event>>(text);
}
if(e.Error==null)
{
字符串文本=e.结果;
var events=JsonConvert.DeserializeObject(文本);
}
我的代码使用我的类事件并从JSON字符串获取事件列表

最好使用Newtonsoft.Json而不是内置库,因为某些现代项目类型(如windows phone应用程序)无法使用这些库

------------------------更新

您可以创建类:

public class Block
{
    public int id;
    public string name;
    public int profileIconId;
    public int revisionDate;
    public int summonerLevel;
}

public class BlockWrapper
{
    public Block block4o;
}
//...
BlockWrapper blockWrapper = JsonConvert.DeserializeObject<BlockWrapper>(text);
公共类块
{
公共int id;
公共字符串名称;
公共国际档案馆;
公共int修订日期;
公共内部传唤级别;
}
公共类区块包装器
{
公共区块4O;
}
//...
BlockWrapper=JsonConvert.DeserializeObject(文本);

我已经从NuGet安装了newtonsoft.json,但是你能告诉我如何使用这个“我的类事件和获取事件列表”吗?我对c#有点陌生,所以请原谅我的问题,当我构建程序时,它是否愚蠢。它打开一个控制台一秒钟,然后关闭它。可能有什么问题?我不确定,但我认为,OpenReadCompletedEventHandler不是您需要的东西。我正在使用DownloadStringCompleted,更改WebClient对象的事件,并将处理程序更改为DownloadStringCompletedEventHander。我认为,回调只适用于正确打开fileclient.DownloadStringAsync(新Uri())在添加callback之后祝您好运:)还有一个想法-当您使用WebClient时,缓存存在一些问题。您将看到您更改了服务器数据,但请求者得到了相同的数据。您可以添加类似“?nocache=randomdouble”的smth url来避免它