0

I want to make a program that reads some data from JSON file generated from an API.
Here's an example of that JSON file:

{"block4o": {
   "id": 20153910,
   "name": "Block4o",
   "profileIconId": 616,
   "revisionDate": 1408783284000,
   "summonerLevel": 30
}}

What I need to do is to get e.g the id and the name from it. Here is my code so far:

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

Any ideas will be really appreciated :)

1 Answer 1

1

You can instal Newtonsoft.Json using NuGet and write smth like this:

if (e.Error == null)
{
    string text = e.Result;
    var events = JsonConvert.DeserializeObject<List<Event>>(text);
}

My code use my class Event and get List of Event from JSON string

It is better to user Newtonsoft.Json instead of build-in libraries, because you can't use them is some modern project types(e.g. windows phone app)

------------------------update

You can create class:

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);
Sign up to request clarification or add additional context in comments.

5 Comments

I've installed newtonsoft.json from NuGet but can you tell me how to use this "my class Event and get List of Event"? I'm a bit new in c# so excuse me for the question if it's stupid
link When I build the program. It opens a console for a sec and then closes it. What could be the problem ?
I'm not sure, but I think, that OpenReadCompletedEventHandler isn't thing, that you need. I'm using DownloadStringCompleted, change event of WebClient object and change handler to DownloadStringCompletedEventHander. I think, that you callback works just for correct opening file
client.DownloadStringAsync(new Uri()) after adding callback
good luck :) one more idea - there are some problems with cache when you use WebClient. you will see that you change server data, but requester get the same. you can add smth like "?nocache=randomdouble" ot url to avoid it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.