Json parsing in windows phone 8 example
This post about how parse the json data . I try to parse the data of my Facebook page data.
Json sample Data
Json URL: http://graph.facebook.com/277606122281580
{"id": "277606122281580",
I have created Button , Progressbar and TextBlock. xzml source code
main.xaml
This post about how parse the json data . I try to parse the data of my Facebook page data.
Json sample Data
Json URL: http://graph.facebook.com/277606122281580
{"id": "277606122281580",
"about": "about android",
"can_post": false,
"category": "Professional services",
"checkins": 0,
"cover": {"cover_id": 512339345474922,"offset_x": 0,"offset_y": 45,"source": "https://scontent-a.xx.fbcdn.net/hphotos-ash3/t1.0-9/11173_512339345474922_179744400_n.png"},
"has_added_app": false,
"is_community_page": false,
"is_published": true,
"likes": 966,
"location": {"city": "Salem","country": "India","street": "thandanur","zip": "636117"},
"name": "Android Beginning For-Beginners",
"phone": "09048382536",
"price_range": "$ (0-10)",
"talking_about_count": 24,
I have created Button , Progressbar and TextBlock. xzml source code
main.xaml
<phone:PhoneApplicationPage
x:Class="VjFirst.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="Chartreuse"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot
is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
Background="LimeGreen">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ContentPanel
- place additional content here-->
<Grid x:Name="ContentPanel"
Grid.Row="1" Margin="3,0,21,0">
<Button Content="Fetch Data From My FB Page" HorizontalAlignment="Left" Margin="62,153,0,0" VerticalAlignment="Top" Click="Button_Click"
/>
<ProgressBar Name="ProgressBarRequest" IsIndeterminate="True" Visibility="Collapsed" HorizontalAlignment="Left" Height="10" Margin="83,84,0,0" VerticalAlignment="Top" Width="100"/>
<TextBlock Name="Category"
HorizontalAlignment="Left" Margin="35,405,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBlock Name="PageName"
HorizontalAlignment="Left" Margin="37,476,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
<TextBlock Name="Phone"
HorizontalAlignment="Left" Margin="35,553,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
<TextBlock Name="Address" HorizontalAlignment="Left" Margin="40,328,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top"/>
</Grid>
</phone:PhoneApplicationPage>
Button Onclick i tried to parse the data and showing in textblock
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using System.Windows.Navigation;
using Microsoft.Phone.Shell;
using VjFirst.Resources;
namespace VjFirst
{
public partial class MainPage : PhoneApplicationPage
{
//
Constructor
public MainPage()
{
InitializeComponent();
}
//}
private void Button_Click(object sender, RoutedEventArgs e)
{
//No
exception handling has been performed in the code and should be adjust as per
requirements.
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted
+= webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility =
System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://graph.facebook.com/277606122281580"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
//Parse JSON result as POCO
var root1 = JsonConvert.DeserializeObject<RootObject>(e.Result);
this.DataContext = root1;
// MessageBox.Show(root1.about + " Total Likes"+root1.likes);
Category.Text = "Category : "+root1.category;
PageName.Text = "Page Name : " +
root1.name;
Address.Text = "Website : " +
root1.website;
Phone.Text = "Likes : " +
root1.likes;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility =
System.Windows.Visibility.Collapsed;
}
}
}
public class RootObject
{
public string about { get; set; }
public string category { get; set; }
public string company_overview { get; set; }
public bool is_published { get; set; }
public string mission { get; set; }
public string products { get; set; }
public int talking_about_count { get; set; }
public string username { get; set; }
public string website { get; set; }
public int were_here_count { get; set; }
public string id { get; set; }
public string name { get; set; }
public string link { get; set; }
public int likes { get; set; }
}
}
No comments:
Post a Comment