如何从Twitter获取日常趋势?

时间:2012-06-06 15:39:51

标签: c# json twitter trend

我想问一下是否有人知道如何提取“名称”和“查询”,并可能将其存储在arraylist中。

源文件:https://api.twitter.com/1/trends/daily.json

2 个答案:

答案 0 :(得分:2)

您可以使用JObject,例如: -

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

答案 1 :(得分:1)

基于这个问题:Parse JSON in C#

创建一个代表您正在提取的JSON的类,并使用链接问题中JSONHelper类中的代码从JSON中提取类:

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