我需要使用json twitter API从推文中获取几条信息。
提供的唯一来源是推文链接。
此解决方案不需要Twitter身份验证,只需要1个库json.net(在Nuget Package Manager中可用)
到目前为止:
其中:
MyTweet = JsonConvert.DeserializeObject(json_data);
using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
namespace parsejson
{
class Program
{
static void Main(string[] args)
{
try
{
Tweet MyTweet = new Tweet();
string url;
url = "https://api.twitter.com/1/statuses/oembed.json?url=https://twitter.com/katyperry/status/657469411700244480";
var json_data = string.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
req.ContentType = "applicaton/json;charset=utf-8";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
json_data = sr.ReadToEnd();
MyTweet = JsonConvert.DeserializeObject<Tweet>(json_data);
var stext = MyTweet.text;
Console.ReadLine();
}
catch (Exception)
{
throw;
}
}
public class Tweet
{
public string created_at { get; set; }
public long id { get; set; }
public string id_str { get; set; }
public string text { get; set; }
public string source { get; set; }
}
}
}