I need to obtain several pieces of information from a tweet using the json twitter API.
The only source provided is a tweet link.
This solution requires no twitter authentication and only 1 library json.net (available in Nuget Package Manager)
So far:
- The program is able to do a httprequest on the Twitter json API.
- I can see a response in the stream
- But I am not able to deserialize the json response into the class.
Where:
- Place a breakpoint in line and You will see that json_data contains json.
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; }
}
}
}