簡體   English   中英

如何在C#中解析Twitter搜索JSON響應?

[英]How to parse Twitter Search JSON response in C#?

我收到了Twitter搜索的JSON響應,但是如何遍歷它們呢?

 protected void BtnSearchClick(object sender, EventArgs e)
    {         
        StringBuilder sb = new StringBuilder();
        byte[] buf = new byte[8192];
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://search.twitter.com/search.json?&q=felipe&rpp=40");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;
        do
        {
            count = resStream.Read(buf, 0, buf.Length);// fill the buffer with data

            if (count != 0)// make sure we read some data
            {
                tempString = Encoding.ASCII.GetString(buf, 0, count);// translate from bytes to ASCII text

                sb.Append(tempString);// continue building the string
            }
        }
        while (count > 0); // any more data to read?

        //HttpContext.Current.Response.Write(sb.ToString()); //I can see my JSON response here
        //object deserializeObject = Newtonsoft.Json.JsonConvert.SerializeObject(sb.ToString());


    }

我會使用dynamic關鍵字

using (WebClient wc = new WebClient())
{

    var json = wc.DownloadString("http://search.twitter.com/search.json?&q=felipe&rpp=40");
    dynamic obj = JsonConvert.DeserializeObject(json);
    foreach (var result in obj.results)
    {
        Console.WriteLine("{0} - {1}:\n{2}\n\n", result.from_user_name, 
                                                 (DateTime)result.created_at, 
                                                 result.text);
    }
}

強類型方式

public class MyTwitterClass
{

    public List<CustomObject> data {get; set;}
}

public class CustomObject 
{

    public string id {get; set;}
    public string name {get; set;}
}

然后,您應該可以執行以下操作:

string someJson=
    @"{""data"":[{""id"":""1"",""name"":""name1""}, {""id"":""2"",""name"":""name2""}]}";

MyTwitterClass someTwitterData = new JavaScriptSerializer().Deserialize<MyTwitterClass>(someJson);

foreach(var item in someTwitterData.data)
{
  Console.Write(item.id + " " + item.name);
}

說了所有您可能想要檢查的內容

http://linqtotwitter.codeplex.com/

謝謝,

以下示例將為您提供幫助,其中“結果”是返回的JSON

        dynamic stuff = JsonConvert.DeserializeObject(result);

        foreach (JObject item in stuff)
        {
            foreach (JProperty trend in item["user"])
            {
                if (trend.Name == "name")
                {
                    MessageBox.Show(trend.Value.ToString());

                }
                else if (trend.Name == "followers_count")
                {
                    // GET COUNT
                }
                else if (trend.Name == "profile_image_url")
                {
                    // GET PROFILE URL
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM