after long hours of searching for a good JSON
library, i found Newtownsoft.json
, so i started using it to decode a json text i get from a web request, i don't know if the json is being decoded properly
the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Request library
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace TestApplication
{
class Connect
{
public string id;
public string type;
private string api = "https://api.stackexchange.com/2.2/";
private string options = "?order=desc&sort=name&site=stackoverflow";
public object request()
{
string totalUrl = this.join(id);
string json = this.HttpGet(totalUrl);
return this.decodeJson(json);
}
private string join(string s)
{
return api + type + "/" + s + options;
}
private string HttpGet(string URI)
{
string html = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.ContentType = "application/json; charset=utf-8";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
return html;
}
private object decodeJson(string json)
{
object js = JsonConvert.DeserializeObject(json);
return js;
}
}
}
the class object is being accessed from the form in this way:
Connect rq = new
rq.id = usernameText.Text;
rq.type = "users";
Debug.WriteLine(rq.request());
i don't know why i can't do rq.request().items
or rq.request()["items"]
, i am still learning c# and i would like to know how to access the json object members in the proper way.
NOTE: this is the first desktop program i'm developing, i am a php/nodejs developer and i wanted to make an application that will connect to stack exchange database and retrieve user's info.