Mega Code Archive

 
Categories / Silverlight / Communication
 

Reading JSON

<UserControl x:Class='SilverlightApplication3.MainPage'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'      xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:d='http://schemas.microsoft.com/expression/blend/2008'      xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'      mc:Ignorable='d'      d:DesignWidth='640'      d:DesignHeight='480'>     <Grid x:Name="LayoutRoot" Background="#FF959595">         <Grid.RowDefinitions>             <RowDefinition Height="25"></RowDefinition>             <RowDefinition Height="25"></RowDefinition>             <RowDefinition Height="25"></RowDefinition>             <RowDefinition Height="*"></RowDefinition>         </Grid.RowDefinitions>         <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,0,0">             <TextBlock Text="Lat:" />             <TextBox x:Name="txLat" Height="22" Width="85" Text="40.78343" />             <TextBlock Text="Long:" />             <TextBox x:Name="txLong" Height="22" Width="85" Text="-73.96625" />             <Button x:Name="btnXML" Content="Get XML" Click="LoadJSON" />         </StackPanel>         <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5,5,0,0">             <TextBlock Text="City:" />             <TextBlock x:Name="txCity" FontSize="12" FontFamily="Courier New" VerticalAlignment="Center" />         </StackPanel>         <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="5,5,0,0">             <TextBlock Text="Name:" />             <TextBlock x:Name="txName" FontSize="12" FontFamily="Courier New" VerticalAlignment="Center" />         </StackPanel>         <StackPanel Grid.Row="3" Margin="5,5,0,0">             <TextBlock Text="Raw Results:" />             <TextBlock x:Name="txtResults" TextWrapping="Wrap" FontFamily="Courier New" FontSize="11" />         </StackPanel>     </Grid> </UserControl> //File: Page.cs using System; using System.IO; using System.Json; using System.Net; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Xml; using System.Runtime.Serialization.Json; namespace SilverlightApplication3 {   public partial class MainPage : UserControl   {     SynchronizationContext uiThread;     public MainPage()     {       InitializeComponent();     }     private void LoadJSON(object sender, RoutedEventArgs e)     {         uiThread = SynchronizationContext.Current;         string uriPath = "http://ws.geonames.org/neighbourhoodJSON?formatted=true&lat={0}&lng={1}&style=full";         Uri uri = new Uri(string.Format(uriPath, txLat.Text, txLong.Text),           UriKind.Absolute);         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);         request.BeginGetResponse(GetResults, request);     }     public void GetResults(IAsyncResult e)     {         HttpWebRequest request = (HttpWebRequest)e.AsyncState;         HttpWebResponse response =           (HttpWebResponse)request.EndGetResponse(e);         Stream responseStream = response.GetResponseStream();         uiThread.Post(UpdateTextJSONObject, responseStream);     }     public void UpdateTextJSONObject(Object stream)     {         JsonObject hood = (JsonObject)JsonObject.Load((Stream)stream);         txCity.Text = hood["neighbourhood"]["city"];         txName.Text = hood["neighbourhood"]["name"];         txtResults.Text = hood.ToString();     }       public void UpdateTextJsonSerializer(object stream)       {           DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyResults));           MyResults hood = (MyResults)ser.ReadObject((Stream)stream);           txCity.Text = hood.neighbourhood.city;           txName.Text = hood.neighbourhood.name;       }   }   public class MyResults   {       public Neighborhood neighbourhood { get; set; }   }   public class Neighborhood   {       public string adminName2 { get; set; }       public string adminCode2 { get; set; }       public string adminCode1 { get; set; }       public string countryName { get; set; }       public string name { get; set; }       public string countryCode { get; set; }       public string city { get; set; }       public string adminName1 { get; set; }   } }