Answered by:
how to Parse Twitter Timeline json

Question
-
Below is the Json response of my twitter .i want to parse the values of "text" and "profile_image_url_https" .
i can able to read the text values using the following code , but how to read the profile image url value from the json response.
class twitter { public string text { get; set; } } protected async void Button2_Click(object sender, EventArgs e) { Button1.Enabled = false; try { string result = await new WebClient().DownloadStringTaskAsync("http://api.twitter.com/1/statuses/user_timeline/gsudhesh.json?count=3"); JavaScriptSerializer ser = new JavaScriptSerializer(); List<twitter> lst = (List<twitter>)ser.Deserialize(result, typeof(List<twitter>)); } catch (Exception) { throw; } Button1.Enabled = true; }
{
"place":null,
"retweet_count":0,
"user":{
"is_translator":false,
"show_all_inline_media":true,
"profile_background_color":"B2DFDA",
"protected":false,
"profile_background_tile":true,
"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1126184785\/P060910_22.19__01__normal.jpg",
"name":"sudhesh",
"profile_sidebar_fill_color":"ffffff",
"location":"",
"screen_name":"gsudhesh",
"listed_count":0,
"utc_offset":19800,
"description":"",
},"text":"happy diwali wishes to all"
}
Sudhesh. G
http://gurucoders.blogspot.com
Answers
-
> Below is the Json response of my twitter .i want to parse the values of "text" and "profile_image_url_https" .
Add Reference to the assembly C:\Program Files\Microsoft SDKs\WCF Support for jQuery\Libraries\Microsoft.Runtime.Serialization.Json.dll (you can download from WCF Support for jQuery)
and use the following code:
var json = @"{ ""place"":null, ""retweet_count"":0, ""user"":{ ""is_translator"":false, ""show_all_inline_media"":true, ""profile_background_color"":""B2DFDA"", ""protected"":false, ""profile_background_tile"":true, ""profile_image_url_https"":""https:\/\/si0.twimg.com\/profile_images\/1126184785\/P060910_22.19__01__normal.jpg"", ""name"":""sudhesh"", ""profile_sidebar_fill_color"":""ffffff"", ""location"":"""", ""screen_name"":""gsudhesh"", ""listed_count"":0, ""utc_offset"":19800, ""description"":""""}, ""text"":""happy diwali wishes to all"" }"; dynamic jo = System.Json.JsonObject.Parse(json).AsDynamic(); var v1 = jo.user.profile_image_url_https.Value; var v2 = jo.text.Value;
- Proposed as answer by Malobukv Friday, October 28, 2011 4:42 PM
- Marked as answer by Bob ShenModerator Monday, November 7, 2011 3:00 AM
-
> it shows only the result as text Value "Default" in my sample code
in your case the 'jo' is an array, thus you should write the following:string v1 = jo[0].user.profile_image_url_https.Value; string v2 = jo[0].text.Value;
orforeach(var itm in jo) { string v1= itm.user.profile_image_url_https.Value; string v2 = itm.text.Value; ... }
- Edited by Malobukv Saturday, October 29, 2011 7:56 AM
- Marked as answer by Bob ShenModerator Monday, November 7, 2011 3:00 AM
All replies
-
> Below is the Json response of my twitter .i want to parse the values of "text" and "profile_image_url_https" .
Add Reference to the assembly C:\Program Files\Microsoft SDKs\WCF Support for jQuery\Libraries\Microsoft.Runtime.Serialization.Json.dll (you can download from WCF Support for jQuery)
and use the following code:
var json = @"{ ""place"":null, ""retweet_count"":0, ""user"":{ ""is_translator"":false, ""show_all_inline_media"":true, ""profile_background_color"":""B2DFDA"", ""protected"":false, ""profile_background_tile"":true, ""profile_image_url_https"":""https:\/\/si0.twimg.com\/profile_images\/1126184785\/P060910_22.19__01__normal.jpg"", ""name"":""sudhesh"", ""profile_sidebar_fill_color"":""ffffff"", ""location"":"""", ""screen_name"":""gsudhesh"", ""listed_count"":0, ""utc_offset"":19800, ""description"":""""}, ""text"":""happy diwali wishes to all"" }"; dynamic jo = System.Json.JsonObject.Parse(json).AsDynamic(); var v1 = jo.user.profile_image_url_https.Value; var v2 = jo.text.Value;
- Proposed as answer by Malobukv Friday, October 28, 2011 4:42 PM
- Marked as answer by Bob ShenModerator Monday, November 7, 2011 3:00 AM
-
hi Malobukv,
you provided the example is working fine. but it shows only the result as text Value "Default" in my sample code
sample code i tested below :
var result = await new WebClient().DownloadStringTaskAsync("http://api.twitter.com/1/statuses/user_timeline/gsudhesh.json?count=3"); dynamic jo = System.Json.JsonObject.Parse(result).AsDynamic();
string url= jo.user.profile_image_url_https.Value;
string url= jo.text.Value;
Sudhesh. G
http://gurucoders.blogspot.com -
> it shows only the result as text Value "Default" in my sample code
in your case the 'jo' is an array, thus you should write the following:string v1 = jo[0].user.profile_image_url_https.Value; string v2 = jo[0].text.Value;
orforeach(var itm in jo) { string v1= itm.user.profile_image_url_https.Value; string v2 = itm.text.Value; ... }
- Edited by Malobukv Saturday, October 29, 2011 7:56 AM
- Marked as answer by Bob ShenModerator Monday, November 7, 2011 3:00 AM
-
Hi Sudhesh,
How's it going? Do you have any updates about this issue?
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.