How to get the daily trend from Twitter?

1.5k Views Asked by At

I would like to ask if anyone know how to extract out the "name" and "query" and maybe store it in a arraylist.

Source file: https://api.twitter.com/1/trends/daily.json

2

There are 2 best solutions below

0
On

Based on this question: Parse JSON in C#

Make a class that represents the JSON you're extracting, and extract the class from the JSON using the code in the JSONHelper class from the linked question:

public class JSONHelper
{
    public static T Deserialise<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        DataContractJsonSerializer serialiser = new DataContractJsonSerializer(obj.GetType());
        ms.Close();
        return obj;
    }
}
3
On

You can use JObject , something like: -

 string response = requestData("https://api.twitter.com/1/trends/daily.json");
 JObject jsonResponse = new JObject();
 var name = string.Empty;
 var query = string.Empty;
 try
 {
       jsonResponse = JObject.Parse(response);
       name = (string)jsonResponse["name"];
       query = (string)jsonRespone["query"];
 }
 catch
 {
       return "";
 }

public string requestData(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string results = sr.ReadToEnd();
    sr.Close();

    return results;
}