(You need to login to Download
source code)
What is Json : - Json means Java
Script Object Notation web service which returns the data in lightweight format
. JSON is a way of representing objects (as also noted in its name).
Difference between xml and json :-
XML Web services poses some serious problems - the heavy payload and the
associated cost in transporting them over the network and parsing them on the
client side make them clunky to work with. A much better approach is to develop
JSON (Java Script Object Notation) Web services, which returns the result in a
lightweight format JSON provides better language-independent representation of
data structures. JSON handles mixed content adequately That's not to say XML's
handling of mixed content is inadequate, only that JSON's minimal mixed content
support isn't a deal breaker.
Consuming Json in Wp7 Application and
parse data : -
Step1 :- Create a New Project with name of jsonservice.
Step 2: - To consume this
JSON Web service in your Windows Phone 7 application, you need to add a
reference to the System.Servicemodel.Web.dll ,System.Servicemodel. and
System.Runtime.Serialization in your project
Step 3 .Add new Class Name AddressList in your project
and just copy Paste following code in
this class
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
namespace JasonService
{
public class AddressList
{
public string
placeName { get; set;
}
public double latitude{
get; set; }
public double lngtitude{
get; set; }
}
[DataContract]
public class Postal_Result
{
[DataMember(Name = "postalcodes")]
public AddressList[]
addresses;
}
}
Step 4.
Now Open Your MainPage.xaml file and Copy Paste
Following Code
<phone:PhoneApplicationPage
x:Class="JasonService.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" d:DesignWidth="480" d:DesignHeight="768"
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="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the
application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY
APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional
content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Click" Height="72" HorizontalAlignment="Left" Margin="278,20,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click"
/>
<ComboBox Width="250" Height="50" Name="PlaceName_combobox" Visibility="Visible" FontSize="24" FontWeight="Bold" Foreground="Black" SelectionChanged="font_combobox_SelectionChanged" Margin="151,119,55,438">
</ComboBox>
<ComboBox Width="250" Height="50" Name="lat_combobox" Visibility="Visible" FontSize="24" FontWeight="Bold" Foreground="Black" SelectionChanged="font_combobox_SelectionChanged" Margin="151,279,55,278">
</ComboBox>
<ComboBox Width="250" Height="50" Name="lan_combobox" Visibility="Visible" FontSize="24" FontWeight="Bold" Foreground="Black" SelectionChanged="font_combobox_SelectionChanged" Margin="151,195,55,362">
</ComboBox>
<TextBlock Height="50" HorizontalAlignment="Left" Margin="12,119,0,0" Name="textBlock1" Text="Place" VerticalAlignment="Top" Width="133" />
<TextBlock Height="50" HorizontalAlignment="Left" Margin="12,195,0,0" Name="textBlock2" Text="Langtitude" VerticalAlignment="Top" Width="133" />
<TextBlock Height="50" HorizontalAlignment="Left" Margin="9,279,0,0" Name="textBlock3" Text="Longtitude" VerticalAlignment="Top" Width="133" />
<TextBlock Height="54" HorizontalAlignment="Left" Margin="12,20,0,0" Name="textBlock4" Text="Click Button To
call Service" VerticalAlignment="Top" Width="260" />
</Grid>
</Grid>
<!--Sample
code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True"
IsMenuEnabled="True">
<shell:ApplicationBarIconButton
IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton
IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem
Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
Step 5 .At last open your
MainPage.xaml.cs page and copy paste following code
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 System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace JasonService
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
PlaceName_combobox.Items.Add("Place");
PlaceName_combobox.SelectedIndex = 0;
}
private void
button1_Click(object sender, RoutedEventArgs e)
{
WebClient wbclient = new
WebClient();
wbclient.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
wbclient.OpenReadAsync(new Uri("http://ws.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT"),
UriKind.Absolute);
}
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs
e)
{
string str = e.Result.ToString();
var data = new DataContractJsonSerializer(typeof(Postal_Result));
Postal_Result postal_obj = (Postal_Result)data.ReadObject(e.Result);
for (int i = 0; i
<= postal_obj.addresses.Length - 1; i++)
{
PlaceName_combobox.Items.Add(postal_obj.addresses[i].placeName.ToString());
lat_combobox.Items.Add(postal_obj.addresses[i]. latitude
.ToString());
lan_combobox.Items.Add(postal_obj.addresses[i]. lngtitude
.ToString());
}
}
private void
font_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PlaceName_combobox.SelectedIndex <= 0)
{
MessageBox.Show("Wait for Service Resopnse ");
}
}
}
}
Step 6 .Now deploys your
application and click on click button to get response of service.
Note : Above code example I used http://ws.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT url
as json web service url ,
So I am really gives thanks provider for
providing such url.
Suggestions & queries are
welcome!
Thanks’
Jaihind Singh
Vincent IT
|