1

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.

Oscar Reyes
  • 4,223
  • 8
  • 41
  • 75

1 Answers1

3

The return type of your request method is object, and so the returned instance will not have a property named items.

You'll need to use generic methods and specify the correct type parameter.

Try changing your decodeJson method to this:

private T decodeJson<T>(string json)
{
    var js = JsonConvert.DeserializeObject<T>(json);

    return js;
}

And then change your request method to this:

public T request<T>()
{
    string totalUrl = this.join(id);

    string json = HttpGet(totalUrl);

    return decodeJson<T>(json);
}

Now write a class with properties that match the name and type of the properties in the JSON returned from the web request.

Then specify the type of this new class as the type parameter for your call to the request method.

For example, if you expected the JSON to contain a string called 'Name' and an int called 'Age', write a class that is something like this:

public class Person
{
    public string Name { get; set; }

    public int Age { get; set; }
}

and then call request like this

Person myPerson = rq.request<Person>();

and you'll be left with an instance of Person with Name and Age properties

lockstock
  • 2,359
  • 3
  • 23
  • 39
  • your method seems to be valid, i am trying to get data from stack exchange api and it has "items" as an array with objects inside which is where im interested to gather, how would i access that data? – Oscar Reyes Nov 24 '14 at 23:55
  • what type of data is found in each of the items in the array? – lockstock Nov 25 '14 at 01:21
  • one of them is an object, the rest is most int, some strings – Oscar Reyes Nov 25 '14 at 01:24
  • ok then create a class that reresents each item (eg `Item`), and pass the type parameter `Item[]` to the request method – lockstock Nov 25 '14 at 01:44